views:

34

answers:

2

I have no clue how to phrase this question in a search engine.

I'm a bit confuse how classes use other class's methods without include/require. I've seen this in PHP a couple of time now. Coming from C++ background, I had to include everything in order to use it, so it's a bit confusing.

But ok here we go:

Say i have improve_db_connection.php:

class improve_db_connection
{

    protected $_connection;

    public function __construct($host, $user, $pwd, $db)
    {
        $this->_connection = @new mysqli($host, $user, $pwd, $db);
        if (mysqli_connect_error()) {
            throw new RuntimeException('Cannot access database: ' .
                mysqli_connect_error());
        }
    }

    public function getResultSet($sql)
    {
        $results = new Pos_MysqlImprovedResult($sql, $this->_connection);
        return $results;
    }
}

The new Pos_MysqlImprovedResult is an instance of Pos_MysqlImprovedResult class, not improve_db_connection. Without declaring the include function to include the class Pos_MysqlImproveResult, the improve_db_connection.php file just knows where the class is.

Is it because the Pos_MysqlImproveResult.php is in the same directory as improve_db_connection.php?

It's the same thing with Zend Framework. But the files are in subdirectories and such. I have a class in application/modules/SF/models that uses another class function that is in application/modules/SF/models/resources. Is it because the Zend naming convention of these classes that Zend just parse through these files and add it into the php's __autoload?

Thank you in advance.

+1  A: 

Zend Framework uses Zend_Loader to automatically load classes when you reference them, as long as it can find them in a logical place.

I assume you are running a Zend Application based script, which automatically configures the Autoloader for you and does all the hard work. As your files are located in their logical folders, and the classes have the ZF convention names, ZF will find them and load them when you reference them.

More information is at the ZF manual:
http://framework.zend.com/manual/en/zend.loader.html
http://framework.zend.com/manual/en/zend.application.html

Valorin
And if it's not a Zend application, then it can still use an autoloader that will be defined somewhere in one of the script files, and can be read to determine what paths it checks before including any necessary files.
Mark Baker
+2  A: 

To add to Valorin's explanation, there's a magic method in PHP that attempts to lookup class definitions when it can't find the class definition. For a detailed explanation, go here: http://php.net/manual/en/language.oop5.autoload.php

The gist of this is:

function __autoload ($class_name) {
    // write your logic to find the class name

    // iterate over the available paths
    foreach(explode(PATH_SEPARATOR, get_include_path()) as $path) {
        // create a local variable representing the file path to the would be include file
        $file = implode(DIRECTORY_SEPARATOR, array(
            $path, "{$class_name}.class.inc",
        ));

        // If the file doesn't exist, continue, avoiding further processing
        if (!file_exists($file)) { continue; }

        // require the file, and return to avoid further processing
        require_once $file;
        return;
    }
}

// provided there's a foo.class.inc file in one of your paths ...
$Foo = new Foo;
Cory Collier