質問

私はモデルインスタンスからモデル名を取得できますか。 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