Reputation: 1791
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);
Upvotes: 0
Views: 110
Reputation: 373
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
Upvotes: 1
Reputation: 6606
$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().
Upvotes: 2