Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is a way to use CLASS_EXISTS and __autoload without CRASH the script?

Tags:

php

exec

autoload

Example:

ClassName.php

<?php echo "This will crash all"; ?>

In another file...

foreach ($FILENAMES_WITHOUT_DOT_PHP as $name => $value) {
    if (class_exists( $value )) {
      echo "ClassName exists...";
    }
    else {
      echo "ClassName doesn't exists....";
    }
}

The output of this code is: This will crash all

Instead of this: ClassName doesn't exists....

Autoload function:

function __autoload( $var_class )
{
     require_once( "$var_class.php") ;
}
like image 932
CRISHK Corporation Avatar asked Sep 28 '10 12:09

CRISHK Corporation


People also ask

What is the use of autoload in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

Which function is used to check if class is exists or not?

The class_exists() function in PHP checks if the class has been defined. It returns TRUE if class is a defined class, else it returns FALSE.

Is class defined PHP?

Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.


2 Answers

Using class_exists will hit the autoloader by default which is why you're seeing your issue. You can bypass the registered autoloader by setting the second param to false.

class_exists('foo', false)

From PHP Documentation

like image 108
Brian Fegter Avatar answered Oct 12 '22 13:10

Brian Fegter


Ok, so here's how it works internally.

When you try to use a class that doesn't exist, it calls each one of the spl_autoload callbacks one by one until the class exists (and the __autoload function is one of them). If it doesn't exist at the end of the chain, it raises the class not found error.

When you call class_exists without the second parameter (which tells it not to try to load it if it doesn't exist), it calls the chain of spl_autoload callbacks until either it finds the class, or the last method is called. Then it returns if it found the class.

So it all depends on what you are doing in the autoload function. If you do something like:

function __autoload($class) {
    $filename = PATH_TO_CLASSES . $class . '.php';
    if (!file_exists($class)) {
        die('Could not find '.$class);
    }
    require_once $filename;
}

It will kill execution and it won't work as intended. Instead, you should do:

function __autoload($class) {
    $filename = PATH_TO_CLASSES . $class . '.php';
    if (file_exists($class)) {
        require_once $filename;
    }
}

That's all you need to do.

Now, you don't want the file to be executed. That's fine. There's an easy solution to that. Don't put that file into the same directory as your autoloaded classes. It defeats the purpose of autoloading.

The only other solution would be to store a map of class names to file names, and base your autoloading off of that. Otherwise it would always execute the file (since that's what you're asking it to do)...

like image 20
ircmaxell Avatar answered Oct 12 '22 12:10

ircmaxell