Autoload imported namespaces in PHP -
the question asked here similar.
i'm trying write autoloader support imported namespaces. directory structure looks (class files listed):
/includes /php config.php shapes.php animals.php /petsmodule /classes cat.php dog.php pcontroller1.php pcontroller2.php /mathmodule /classes triangle.php square.php mcontroller1.php mcontroller2.php
all of controllers include config.php file, has autoloader code:
<?php spl_autoload_register(function($fqclassname) { $fqclassname = ltrim($fqclassname, '\\'); $lastslash = strrpos($fqclassname, '\\'); if($lastslash) { // class in namespace require '/' . str_replace('\\', '/', substr($fqclassname, 0, $lastslash)) . '/classes/' . substr($fqclassname, $lastslash + 1) . '.php'; } else { // class global require "/includes/php/$fqclassname.php"; } }, true);
now works fine basic cases, fails when start importing namespaces. instance:
<?php namespace mathmodule; use petsmodule; $t = new triangle(60, 60, 60); // works fine $fluffy = new cat("fluffy"); // fails
the fully-qualified class name gets passed autoloader function mathmodule\cat
, fair assumption.
is there way make autoloader work imported namespaces? i'm guessing there has way since autoloader similar 1 psr-0.
autoloading not matter.
to use new cat("fluffy")
should write use petsmodule\cat;
or use petsmodule\cat cat;
http://www.php.net/manual/en/language.namespaces.importing.php
Comments
Post a Comment