Namespaces in Provider Files (CakePHP2.x and PHP5.4.3)

I want to use a css parser that uses namespaces. I put the files in the providers and application by importing them. but the script itself does not find its classes

At the top of my class, I import the file:

App::import('Vendor', 'Sabberworm', array('file' => 'Sabberworm/CSS/Parser.php')); 

which is located in / root / vendors / Sabberworm / CSS / (all files are in this namespace)

Inside the class method, I create a new instance:

 public function parse($content) { $oParser = new Sabberworm\CSS\Parser($content); ... } 

So far so good. But if now I want to call $oCss = $oParser->parse(); fatal errors:

 "Fatal error: Class 'Sabberworm\CSS\CSSList\Document'" 

it does not work because it needs other files (which must be loaded using namespaces). the root providers folder is in the include path, and the external script appears to set the namespace to "Sabberworm \ CSS namespace;". What am I missing? I am new to namespaces.

+4
source share
1 answer

Add this to bootstrap

 spl_autoload_register(function($class) { foreach(App::path('Vendor') as $base) { $path = $base . str_replace('\\', DS, $class) . '.php'; if (file_exists($path)) { return include $path; } } }); 

Or just inside a function:

 $path = APP . 'Vendor' . DS . str_replace('\\', DS, $class) . '.php'; if (file_exists($path)) { include $path; } 
+9
source

All Articles