Question

I'm noticing that adding the include path of a folder I need is not enough to access the classes.

I have a folder application/tests/ which contains application/tests/Test.php

class Test.php is named Test without any prefixes

class Test {

}

If I add the tests folder only in the include path

realpath(APPLICATION_PATH . '/tests')

The class Test.php doesn't work in the controllers

new Test()

so isn't including the folder in the include path enough?

Was it helpful?

Solution

@StasM has it right. But here's a little more explanation.

The include_path tells PHP where to look when it executes an include, an include_once, a require, or a require_once. Of course, these includes and requires always reference a particular file.

Autoloading is specifically about missing classes. When an as-yet-unloaded class is referenced in code - typically, though not exclusively, by invoking the new operator, as in your example - then autoloading is a process that can kick in with an algorithm for loading the class. This process typically involves starting from the name of the missing class and producing a collection of possible pathnames - either absolute in the filesystem or relative to entries in the include_path - from the name of the missing class and then doing performing an include in the hopes that the class definition resides in one of them.

In your case, you have called for a class named "Test". The file containing this class resides in the file APPLICATION_PATH . 'tests/Test.php'. And you have placed the directory APPLICATION_PATH . 'tests' in the include_path. But until there is an autoloading algorithm in place, there is no way for the system to connect the class name 'Test' to the file name 'Test.php'.

The PEAR-style class name convention would provide this connection. And an autoloader that employs that convention - like the default Zend Framework autoloader - would be able to perform that mapping from class name to file name, and then include the required file.

As a final complication, the conventional Zend Framework directory layout convention places certain classes in folders that are NOT on the include path: models, forms, services, controllers, view helpers, action helpers, etc. That's why you will often see more configuration of the autoloader - typically in the Bootstrap class - defining a mapping between certain classnames and certain places in the filesystem that are off the include_path.

For example, a class named 'Default_Model_User' might - at first glance - be expected to reside on the include_path in a file 'Default/Model/User.php'. But the standard app directory structure wants to place that file in 'application/models/User.php'. Note the plural 'models' and the lowercase 'm' in the path name, as well as the presence of "Default" in the classname. Extra config on the autoloader is required in order to make sense of that, the provide a pattern for the classname-to-filename mapping. This is usually accomplished with a resource autoloader.

Hope any of this helps, either you or someone else. Cheers!

OTHER TIPS

Include path shows where PHP would look for scripts that it is about to include - so that when you say include('blah.php') PHP engine would look for blah.php in all paths in the include path.

Autoloading is the functionality that allows certain function to be called when PHP detects that class is needed but not defined. In this function, you can use the include path to find the file containing the class and load it, but these are absolutely different things.

realpath() is not related to any of those and is a function that returns unique filesystem path of a given file, with things like .. and symbolic links resolved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top