note 126010 added to function.spl-autoload-call
| From: | Melnofil at main dot php dot net | Date: | Thu, 15 Apr 2021 10:23:58 +0000 |
| Subject: | note 126010 added to function.spl-autoload-call | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216284@lists.php.net to get a copy of this message | ||
A complete example with namespaces:
fruits/pinapple.php
<?php
namespace Fruits;
echo "pinapple\n";
class Pinapple { }
?>
fruits/pinapple.php
<?php
namespace Vegetables;
use Fruits\Pinapple;
echo "carrot\n";
class Carrot { }
new Pinapple(); // Let's call autoload here
?>
index.php
<?php
spl_autoload_register(function($class_name) {
@include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/',
$class_name)) . '.php');
});
new Vegetables\Carrot();
?>
Result:
carrot
pinapple
index2.php
<?php
spl_autoload_register(function($class_name) {
@include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/',
$class_name)) . '.php');
});
spl_autoload_call('Fruits\\Pinapple'); // Reverse the load order
spl_autoload_call('Fruits\\Pinapple'); // Multiple call is safe with include_once
new Vegetables\Carrot();
?>
Result:
pinapple
carrot
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 2a01:e0a:199:abf0:42::)
----
Manual Page -- https://php.net/manual/en/function.spl-autoload-call.php
Edit -- https://main.php.net/note/edit/126010
Del: integrated -- https://main.php.net/note/delete/126010/integrated
Del: useless -- https://main.php.net/note/delete/126010/useless
Del: bad code -- https://main.php.net/note/delete/126010/bad+code
Del: spam -- https://main.php.net/note/delete/126010/spam
Del: non-english -- https://main.php.net/note/delete/126010/non-english
Del: in docs -- https://main.php.net/note/delete/126010/in+docs
Del: other reasons-- https://main.php.net/note/delete/126010
Reject -- https://main.php.net/note/reject/126010
Search -- https://main.php.net/manage/user-notes.php