Yii Framework Forum: Namespace Confusion In Yii2 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Namespace Confusion In Yii2 Rate Topic: ***** 1 Votes

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 May 2013 - 08:57 AM

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:

Quote

Inside a namespace, when PHP encounters a unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, One must refer to them with their fully qualified Name [...]
For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.


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?
0

#2 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 17 May 2013 - 09:48 AM

It's probably because there's

namespace yii\base;
use Yii;

scattered across the framework?
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,895
  • Joined: 04-October 08
  • Location:DC, USA

Posted 17 May 2013 - 10:10 AM

It doesn't work for me.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 May 2013 - 03:06 AM

Oh, you're right. I looked at the wrong action in my code. But then all the Yii::$app->... will also not work in the demo application.

So the rule is: You can write Yii::$app in non-namespaced code, like e.g. view files. Everywhere else it must be \Yii::$app. But maybe we should not differ at all and use \Yii::$app everywhere? Alternatively the namespaces could remove from the app code again.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users