3

Now I am working with yii framework and I'd like to wrote something like this:

protected static $model = "Customer";
...
public function actionIndex() {
    $model::model()->find(...

Now it works:

protected static $model = "Customer";
protected static $model_obj;
...
public function __construct($controller, $id) {
    $this->model_obj = new self::$model;
...
public function actionIndex() {
    $model_obj::model()->find(...

but creating object for access static member is a bad thing. how to avoid it?

getClass takes object as first parameter and it is not suitable for this purpose

google say:

$a = constant($myClassName . "::CONSTANT");
$b = call_user_func(array($myClassName, "static_method"));

it looks like a horrible peace of shit. using this may make many troubles. another solution?

oh! my problem was another:

$controller::$NAME::model() // error

$controller_name = $controller::$NAME
$controller_name::model() // good

thanks

2 Answers 2

6
class foo
{
  public static function bar()
  {
    return 42;
  }
}

// class name as string

$class = 'foo';

var_dump($class::bar()); // 42

// method name as string

$method = 'bar';

var_dump(foo::$method()); // 42

// class AND method names as strings

var_dump($class::$method()); // 42
Sign up to request clarification or add additional context in comments.

1 Comment

If this answered your posted question, please remember to accept as the answer and post your other problem in a new question (so that it may help other users as well)
0
call_user_func(array($myClassName, "static_method"));

Is the primary way to do it. I'm not quite sure why this would cause any problems.

1 Comment

compare call_user_func(array("Customer", "read", $id)); with Customer::read($id) =(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.