0

I have the following:

User::model()->exists($someParamsHere);

Is there a way to make the class name 'User' dynamic? So something like this:

$className::model()->exists($someParamsHere);

But that doesn't seem to work.

I also read something about the ReflectionClass, but i'm not really sure how to use it.

I tried this, but off course the model() method is never called this way:

$reflectionMethod  = new ReflectionMethod($className, 'exists');
$reflectionMethod->invoke(null, $someParamsHere);

2 Answers 2

2

$className::model() works with PHP 5.3 and above, if I'm not mistaken. A workaround is to use CActiveRecord::model($className). See the documentation for CActiveRecord.model().

Sign up to request clarification or add additional context in comments.

1 Comment

My mistake! I used $this->className as the variable, but it gives an error on the '->'. Putting this in another variable works. $className = $this->className; and then $className::model()->exists();. Great answer though.
1

In PHP >= 5.3 works fine:

<?php
class Foo {

    static function Bar() {
        return "Bar";
    }

    static function getFoo() {
        return new static();
    }

    function getBar() {
        return static::Bar();
    }
}

$class = "Foo";

print $class::Bar() . "\n";

print $class::getFoo()->Bar() . "\n";

Result:

Bar
Bar

Comments

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.