Welcome Guest, Not a member yet? Register   Sign In
__autoload
#1

[eluser]Moon 111[/eluser]
I would like to create an __autoload function. Where should I put it?
#2

[eluser]Moon 111[/eluser]
I'm thinking of creating a presystem hook?
#3

[eluser]Rick Jolly[/eluser]
Good question. I don't know what the most logical place would be. Obviously, an autoload function could be in any file that is included before the classes you wish to autoload. A hook is a fine solution if you are planning to use the autoload in all your applications (a system extension rather than an application-specific extension). The simplest method would be to put the autoload in index.php or config.php, but I'm not sure if that's the most logical or transparent place.
#4

[eluser]Moon 111[/eluser]
Quote:A PHP Error was encountered
Severity: Warning

Message: include(C:\wamp\www\CodeIgniter/system/application/models/ci_db.php) [function.include]: failed to open stream: No such file or directory

Filename: codeigniter/CodeIgniter.php

Line Number: 193

I put it where it would call the pre_controller hooks...

What's wrong?
#5

[eluser]Moon 111[/eluser]
I updated so that it ignored anything that couldn't be found in the models folder.

Code:
<?php

function __autoload($classname) {

    $handle = opendir(APPPATH . 'models/');
    
    while (false !== ($file = readdir($handle))) {
        $file_array[] = $file;
    }
    
    if(in_array(strtolower($classname) . '.php', $file_array))
        include APPPATH . 'models/' . strtolower($classname) . '.php';
}

?>
#6

[eluser]Rick Jolly[/eluser]
That's terribly inefficient. Use naming conventions instead. If only models have the word "Model" in their name, then:
Code:
if(stristr($classname,'model'))
{
    require_once(APPPATH . 'models/' . $classname . EXT);
}




Theme © iAndrew 2016 - Forum software by © MyBB