範例 Namespaces 命名空間 與 Class Autoloading 自動加載
<!-- index.php --> <? require 'autoload.php'; ?> <? use sm\model\ns1 as ns1; $obj1 = new ns1; $obj2 = new testnew(); var_dump($obj1->index()); var_dump($obj2->index()); var_dump(ns1::index()); var_dump(sm\model\ns1::index()); ?> <!-- autoload.php --> <? spl_autoload_register(function ($class_name) { include $class_name . '.php'; }); ?> <!-- sm/model/ns1.php --> <? namespace sm\model; class ns1 { public function index() { return 'ns1'; } } ?> <!-- sm/testnew.php --> <? class testnew { public function index() { return 'test'; } } ?>
參考文章: https://ithelp.ithome.com.tw/articles/10134247