문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

add this method to your State Class

public function getModelName()
{
    return __CLASS__;
}

and call it like this:

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

Use this PHP method : 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