Home  >  Article  >  php教程  >  快速实例化一个类 第二版

快速实例化一个类 第二版

WBOY
WBOYOriginal
2016-06-07 11:44:541081browse

快速实例化一个类,
/**<br>  * 快速实例化App/Lib/Class目录(默认)下的一个类,<br>  * <p><br>  * _D('ViewModel.AbcUserViewModel');<br>  * 实例化App/Lib/ViewModel/AbcUserViewModel.class.php<br>  * </p> <br>  * @param string $name    文件夹.文件名//文件名也是类名 (.calss.php前半部分)<br>  * @param string $layer   目录层名称/ 默认为空<br>  * @param string $ext     文件后缀<br>  * @return object<br>  */<br> function _D($name = '', $layer = '', $ext = '.class.php') {<br>     if (empty($name))<br>         die('类名不能为空!(本消息来至:_D)');<br>     $layer = $layer ? $layer . '/' : '';<br>     if (strpos($name, '.')) {<br>         $path = explode('.', $name);<br>         $name = $path[1];<br>         $path = './App/Lib/' . $path[0] . '/' . $layer;                         //指定其它路径<br>     } else {<br>         $path = './App/Lib/Class/' . $layer;                                    //默认加载路径:/Class<br>     }<br>     import($name, $path, $ext);<br>     if (class_exists($name))<br>         $model = new $name();<br>     else<br>         die($name . '类不存在(来消息来至:_D)');<br>     return $model;<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn