1

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

Big Bird
  • 257
  • 2
  • 9
  • 5
    There is no reason for the autoloader to run if the class has been defined in the same script in which it is used (as in your example). If this is really what's happening check for typos in your class names. – Tim Fountain May 07 '12 at 17:08

2 Answers2

1

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Non-conforming wasn't the right word choice, 'conflicting' is. My class name looks like an autoloading class, but I don't want it auto loaded. Upon further consideration, this might not even be possible; I just don't understand PHP and class loading well enough. Are the actual class definitions persisted across script evaluations? Or does every trip through a script re-find the classes used and load them again? Forgive my ignorance, but this is something that's always been a bit of a mystery to me. – Big Bird May 07 '12 at 18:36
  • 1
    @BigBird PHP is an shared-nothing so yes, each Request will re-find the classes and load and interpret them again. Autoloaders work by intercepting any calls to unknown class names and providing strategies to include them. This means that if you include your conflicting class before a call to the class is made, the Autoloader will not try to load it because it is already included. – Gordon May 07 '12 at 21:08
0

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.

Chris
  • 884
  • 5
  • 8