I’m confused why in Yii 2 we can access the global Yii class from inside a namespace without a “\”.
The PHP manual clearly states:
So for unqualified class names (Names without any "\") it should not fall back to global context, if you are inside a namespace.
Now i wonder, why this works:
<?php
namespace app\controllers;
use yii\web\Controller;
class SiteController extends Controller
{
public function actionTest()
{
echo Yii::t('test','test');
}
There’s no \
in front of Yii:t()
. So according to the above quote this should be resolved to app\controllers\Yii::t() and throw an error. But it doesn’t!
I’ve created the same scenario with 2 simple files:
foo.php
<?php
// No namespace - so we define a global class
class foo {
public static function x() {
echo "x";
}
}
bar.php
<?php
namespace bar;
include "foo.php";
// Unqualified class name is resolved to bar\foo::x() and gives an error
foo::x();
Here i do get an error. So what am i missing from Yii2?