11

如何从模型实例中获取模型名称。例如

$model=新状态;

这里,State is model $model 是 State 模型实例。

我想从 $model 即模型实例中获取模型名称,即状态。

4

4 回答 4

15

将此方法添加到您的状态类

public function getModelName()
{
    return __CLASS__;
}

并这样称呼它:

$model = new State();
echo $model->getModelName();
于 2010-05-13T11:50:38.823 回答
13

get_class() — 返回对象的类名

字符串 get_class ([ 对象 $object ] )

因此你可以这样使用它: $modelname=get_class($modelinstance);

-> 它返回一个字符串。

于 2010-08-16T19:50:20.220 回答
1

使用这个 PHP 方法:get_class

 print get_class($object);
于 2013-11-19T09:28:03.660 回答
0
<?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 
}   
于 2014-05-27T19:59:34.863 回答