题
我怎样才能从模型实例型号名称。 对于e.x。
$模型=新的国家;
这里, 状态模型 $模式是国家的模型实例。
我想从$模型即模型实例获得型号名称即国家。
解决方案
get_class() - 返回类的对象
的名称的字符串get_class([对象$对象])
因此使用这样的:$ MODELNAME = get_class($ modelinstance);
- >它返回一个字符串
其他提示
添加此方法以您的国家类
public function getModelName()
{
return __CLASS__;
}
和调用它是这样的:
$model = new State();
echo $model->getModelName();
使用此方法PHP:get_class
print get_class($object);
<?php
class Item extends CActiveRecord
{
public function getBaseModelName()
{
return __CLASS__;
}
public function getCalledClassName()
{
return get_called_class();
}
}
class Product extends Item {}
class Service extends Item {}
class ProductController extends CController
{
$model = new Product;
echo $model->baseModelName; // Item
}
class ServiceController extends CController
{
$model = new Service;
echo $model->calledClassName; // Service
echo get_class($model); // Service
}
不隶属于 StackOverflow