在目录结构如下的php怎么自动加载呢? namesapce 看我的代码: <?php #framework/app.class.php namespace Framework; /** * php namespace test */ class app { function __construct() { } public static function niubai() { echo "this is niubai"; } }
<?php #home.php 注意:home.php与framework同级 namespace  home; use Framework\app; spl_autoload_register(function ($class) { if ($class) { $file = str_replace('\\', '/', $class); $file .= '.class.php'; //也可以不带class,同理在framework下的app.class.php的class要去掉。 if (file_exists($file)) { include $file; } } }); $app = new app(); $app->niubai(); // app::niubai(); //也可以这样子调用,哈,应为是static的。 ok,大功告成,访问home.php就可以后到输出啦:this is niubai. 要在home.php中加spl_autoload_register这个函数才能自动加载。

分类: web

标签: