tags:

views:

44

answers:

5

So I have an idea, but I'm thinking I need to run it by StackOverflow before I do something stupid.

I want to have an associative array of class names => filepaths. If PHP ever runs into a fatal error where the class is not defined, it will check if the key exists in my array and then require_once the class. This will prevent unnecessary bulk loading of classes that may never be used.

Bad idea?

+4  A: 

How about trying PHP's built in autoloading.

Tesserex
+1  A: 

If you are on PHP5, you can use __autoload().

makes your code a bit more manageable , although performance-wise, it's a bad choice. But I wouldn't worry it unless I'm building a Facebook.

rubayeet
+1  A: 

What you are trying to do is already handled by the php __autoload function. You can read all about it here: http://php.net/manual/en/language.oop5.autoload.php

So, not a bad idea at all ;)

Johan
+1  A: 

Autoloading is the right way to do it, but spl_autoload_register is a cleaner way than __autoload, because it allows multiple autoloaders. Function __autoload also AFAIK stops working when spl_autoload_register is called, unless __autoload is also registered.

You can write your own autoload or use an existing one. For example, Zend Framework has an autoloader that uses conventions (Foo_Bar is in Foo/Bar.php). Nette Framework has RobotLoader, that indexes your classes and uses the index when neccessary. However, unless you use other things from the framework, it is probably too large.

see: http://www.php.net/manual/en/function.spl-autoload-register.php

v6ak
this is what i have explained in my post exactly.
RobertPitt
+1  A: 

you should use autoloading with specified clas name structure, here is an example

the class names should should be only alpha and _ case-insensitive.

Lets take this directory structure and files

/classes/class.php
/classes/input/input.php
/classes/output/output.php
/classes/regex/regex.php
/classes/interface/parser/interface_parser.php
/classes/parser/parser.php
/classes/parser/xml/parser_xml.php
/classes/parser/html/parser_html.php

having the structure like this is good as it encourages you to code better when it comes to OOP.

Now if we take a look at the /classes/parser/html/html_parser.php file:

class Parser_Html extends Parser implements Interface_Parser
{
    //looks nice in here
}

usually you would have to make sure the interface and the extended class is loaded, but these get autoloaded as well if they have not already.

creating the auto load system for this is not that complex, its just 1 function.

function __autoload($name)
{
    //Classes
    $parts = explode('_',strtolower($name));

    $path = '/classes/';
    foreach($parts as $p)
    {
        $path .= $p;
    }
    $path .= '/' . $name . '.php';

    if(file_exists($path))
    {
        require_once $path;
    }
}

so instead of including the class file first just run the class initiation.

$HtmlParser = new Parser_Html();

as the file has not been include the __autoload is run with a param of the class name, the autoload then looks in the directory that's relevant to the class name to try and load it.

also as your using the extend keyword in the class file shown above the class that is to be the parent gets run threw trhe autoloader aswell so you do not need to pre-load interfaces and classes etc.

Hope this helps you.

Note: All code provided is untested and written for informational purposes, I would recommend you research the techniques more in detail before any implementation is done.

RobertPitt