- <?php
- namespace Core;
- class Model {
- private $table;
- public function __construct($table='') {
- if($table!='') //直接给基础模型传递表名
- $this->table=$table;
- else { //实例化子类模型
- $this->table=substr(basename(get_class($this)),0,-5);
- }
- echo $this->table,'<br>';
- }
- }
- namespace Model;
- class ProductsModel extends \Core\Model{
-
- }
- namespace Controller\Admin;
- new \Core\Model('news'); //news
- new \Model\ProductsModel(); //Products
复制代码小结: 1、get_class():获取对象的类(包括命名空间) 2、substr():截取字符串,-5表示字符串的最后5个字符忽略
|