Getting model name from model instance YII

19,858

Solution 1

get_class() — Returns the name of the class of an object

string get_class ([ object $object ] )

therefore you use it like this: $modelname=get_class($modelinstance);

->it returns a string.

Solution 2

add this method to your State Class

public function getModelName()
{
    return __CLASS__;
}

and call it like this:

$model = new State();
echo $model->getModelName();

Solution 3

Use this PHP method : get_class

 print get_class($object);
Share:
19,858

Related videos on Youtube

Mohd Viqar
Author by

Mohd Viqar

Updated on May 28, 2020

Comments

  • Mohd Viqar
    Mohd Viqar almost 4 years

    How can i get model name from model instance. For e.x.

    $model=new State;

    here, State is model $model is State model instance.

    I want to get model name i.e State from $model i.e model instance.

  • hpaknia
    hpaknia almost 11 years
    Hi @Wager, Also I prefer toninoj more, why don't you accept an answer as true right now?

Related