封装函数获取表名案例代码
<?phpnamespace 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个字符忽略
页:
[1]