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
        {
            $namespace=str_replace('\\','.',ltrim($className,'\\'));
            if(($path=self::getPathOfAlias($namespace))!==false)
                include($path.'.php');
            else
                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)
	{
		if(isset(self::$_aliases[$alias]))
			return self::$_aliases[$alias];
		else if(($pos=strpos($alias,'.'))!==false)
		{
			$rootAlias=substr($alias,0,$pos);
			if(isset(self::$_aliases[$rootAlias]))
				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)
			{
				if(self::$_app->findModule($rootAlias)!==null)
					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?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,780
  • 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 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#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:

https://gist.github.com/4234540
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