如何从模型实例中获取模型名称。例如
$model=新状态;
这里,State is model $model 是 State 模型实例。
我想从 $model 即模型实例中获取模型名称,即状态。
将此方法添加到您的状态类
public function getModelName()
{
return __CLASS__;
}
并这样称呼它:
$model = new State();
echo $model->getModelName();
get_class() — 返回对象的类名
字符串 get_class ([ 对象 $object ] )
因此你可以这样使用它: $modelname=get_class($modelinstance);
-> 它返回一个字符串。
使用这个 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
}