我怎样才能从模型实例型号名称。 对于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 
}   
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top