Yii Framework Forum: Autoloading namespaced libraries is disappointing - Yii Framework Forum

Jump to content

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

Autoloading namespaced libraries is disappointing Yii and PHP 5.3 namespace autoloading Rate Topic: -----

#1 User is offline   newyiiuser 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-October 11

Posted 07 October 2011 - 01:20 PM

I tried to put together an application by using 3rd party vendor modules loaded by namespace.

Unfortunately this has turned out to be a non-start due to the fact that I can't depend on the namespaces to resolve in a way that's conducive to conventional use of namespaces.

I have found that namespaces defined through setPathOfAlias will only resolve if each file is defined explicitly. This is not a desirable trait, in fact it’s counter intuitive.

This is the block of code in Yii that resolves namespaces from framework/YiiBase.php Line 423:

        else  // class name with namespace in PHP 5.3
                return false;
        return class_exists($className,false) || interface_exists($className,false);

here is the getPathOfAlias method:

	 * Translates an alias into a file path.
	 * Note, this method does not ensure the existence of the resulting file path.
	 * It only checks if the root alias is valid or not.
	 * @param string $alias alias (e.g. system.web.CController)
	 * @return mixed file path corresponding to the alias, false if the alias is invalid.
	public static function getPathOfAlias($alias)
			return self::$_aliases[$alias];
		else if(($pos=strpos($alias,'.'))!==false)
				return self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
			else if(self::$_app instanceof CWebApplication)
					return self::getPathOfAlias($alias);
		return false;

It would appear that the result of getPathOfAlias will be false unless the exact class (down to the class name) is defined as an alias. This not the way namespace autoloading should be done. Is there some solution to this?

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,225
  • Joined: 17-January 09
  • Location:Russia

Posted 13 October 2011 - 05:56 AM

You can either create a patch or simply register your own auto loader.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark

#3 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 07 December 2012 - 12:05 PM

This may be helpful:


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