封装函数获取表名案例代码

[复制链接]
查看1604 | 回复0 | 2020-11-24 10:03 | 显示全部楼层 |阅读模式
  1. <?php
  2. namespace Core;
  3. class Model {
  4.         private $table;
  5.         public function __construct($table='') {
  6.                 if($table!='')                //直接给基础模型传递表名
  7.                         $this->table=$table;
  8.                 else {                                //实例化子类模型
  9.                         $this->table=substr(basename(get_class($this)),0,-5);
  10.                 }

  11.                 echo $this->table,'<br>';
  12.         }
  13. }

  14. namespace Model;
  15. class ProductsModel extends \Core\Model{
  16.        
  17. }
  18. namespace Controller\Admin;

  19. new \Core\Model('news');                        //news
  20. new \Model\ProductsModel();                        //Products
复制代码
小结:
1、get_class():获取对象的类(包括命名空间)
2、substr():截取字符串,-5表示字符串的最后5个字符忽略

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16