Autoloading namespaced libraries is disappointing

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?

You can either create a patch or simply register your own auto loader.

This may be helpful: