Skip to content

Instantly share code, notes, and snippets.

@cebe
Last active April 13, 2019 06:26
Show Gist options
  • Save cebe/6206940 to your computer and use it in GitHub Desktop.
Save cebe/6206940 to your computer and use it in GitHub Desktop.
PHP autoload function return values do not make any sense!
<?php class A {}
<?php
spl_autoload_register(function($class) {
echo "autoloader 1\n";
if ($class == 'A')
include 'A.php';
if ($class == 'C')
return true;
if ($class == 'D')
return false;
});
spl_autoload_register(function($class) {
echo "autoloader 2\n";
if ($class == 'B')
include 'B.php';
return true;
});
spl_autoload_register(function($class) {
echo "autoloader 3\n";
});
echo "instantiate A...\n";
$a = new A();
echo "instantiate B...\n";
$b = new B();
echo "instantiate C...\n";
if (class_exists('C'))
echo "C exists\n";
echo "instantiate D...\n";
if (class_exists('D'))
echo "D exists\n";
<?php class B {}
@cebe
Copy link
Author

cebe commented Aug 11, 2013

Output:

instantiate A...
autoloader 1
instantiate B...
autoloader 1
autoloader 2
instantiate C...
autoloader 1
autoloader 2
autoloader 3
instantiate D...
autoloader 1
autoloader 2
autoloader 3

@cebe
Copy link
Author

cebe commented Aug 11, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment