class_exists is running spl_autoloader_register

Asked

Viewed 55 times

2

I created a simple script to automatically load classes using spl_autoload_register, but I noticed a strange behavior when I use class_exists the spl_autoload_register is executed, example:

<?php
function autoLoadClass($name) {
    echo 'spl_autoload_register: ', $name, '<br>';
}

spl_autoload_register('autoLoadClass');

class_exists('Foo');
class_exists('Bar');
class_exists('Foo\\Bar');

Exit:

spl_autoload_register: Foo
spl_autoload_register: Bar
spl_autoload_register: Foo Bar

Is that correct? Is there any way to do the spl_autoload not be called when using class_exists?

1 answer

3


According to the PHP Handbook, just set the second parameter of this function to FALSE.

Behold:

class_exists('Foo', false);

See the skeleton of this function:

bool class_exists ( string $class_name [, bool $autoload ] )

That is, if you set the second parameter as FALSE, this function does not perform automatic class loading. By default it is set to TRUE.

  • Lack of attention my kk. Truth. ~ 9min to accept the answer

  • 1

    This time, in the Manual, I was right. There is usually this information in English and not in Portuguese. The best option is to ask in the same SOPT :p

Browser other questions tagged

You are not signed in. Login or sign up in order to post.