Reputation: 267
Within a Zend app, is there a way to define a class which does not conform to autoloading naming conventions and still have it recognized when I try to use it later on? I imagine you define the class and register it, somehow, with the autoloader, but after reading the autoloader manpage, I still don't understand how to do it.
My use case is during testing (and no, I don't want a mock or stub in this case). I define a new class at the top of my test script, but when my application code references that class, the autolader aggressively tires to load it based on naming convention.
<?php
class Non_Conforming_Dummy_Class
{
}
// should I register this new class with the autoloader right here?
class Whatever
{
public function useIt()
{
$class = new Non_Conforming_Dummy_Class;
}
}
I get a 'no such file or directory' which looks for Non/Conforming/Dummy/Class.php
Upvotes: 1
Views: 160
Reputation: 884
The autoloader is a fallback mechanism when PHP detects a class that it hasn't been registered, so if your code is like the one at the top, it shouldn't fail, since the class is getting registered in the same script, ej:
class foobar{}
class demo
{
public function foo()
{
$bar = new foobar();
}
}
works without a problem, in case your class is in another file, you can include() it, or require() it, the autoloader will be the last function to be called when the class is not found.
Upvotes: 0
Reputation: 317177
The ZF Autoloader will load any classes following PSR-0 convention. Any classes not following that convention cannot be loaded with the Autoloader. However, you can
register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces).
Zend_Loader_Autoloader
will attempt to match these first before using its internal autoloading mechanism.
This means, you can add your own autoloader that knows your naming scheme to ZF's autoloader:
$autoloader->pushAutoloader(array('myAutoloader', 'autoload'), 'MyPrefix');
See http://framework.zend.com/manual/en/zend.loader.autoloader.html
Upvotes: 1