Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用变量作为类名_Php_Class_Yii - Fatal编程技术网

Php 使用变量作为类名

Php 使用变量作为类名,php,class,yii,Php,Class,Yii,我有以下资料: User::model()->exists($someParamsHere); 有没有办法使类名“User”动态化?比如说: $className::model()->exists($someParamsHere); 但这似乎不起作用 我也读了一些关于ReflectionClass的文章,但我不确定如何使用它 我尝试了这个方法,但显然model()方法从未以这种方式调用: $reflectionMethod = new ReflectionMethod($cla

我有以下资料:

User::model()->exists($someParamsHere);
有没有办法使类名“User”动态化?比如说:

$className::model()->exists($someParamsHere);
但这似乎不起作用

我也读了一些关于ReflectionClass的文章,但我不确定如何使用它

我尝试了这个方法,但显然model()方法从未以这种方式调用:

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

$className::model()
适用于PHP5.3及以上版本,如果我没有弄错的话。解决方法是使用
CActiveRecord::model($className)
。请参阅文档。

$className::model()
适用于PHP5.3及以上版本,如果我没有弄错的话。解决方法是使用
CActiveRecord::model($className)
。请参阅文档。

$className::model()
适用于PHP5.3及以上版本,如果我没有弄错的话。解决方法是使用
CActiveRecord::model($className)
。请参阅文档。

$className::model()
适用于PHP5.3及以上版本,如果我没有弄错的话。解决方法是使用
CActiveRecord::model($className)
。请参阅PHP>=5.3中的。

的文档:

<?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";

在PHP中>=5.3可以正常工作:

<?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";

在PHP中>=5.3可以正常工作:

<?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";

在PHP中>=5.3可以正常工作:

<?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";

我错了!我使用$this->className作为变量,但它在'->'上给出了一个错误。把这个放在另一个变量中就行了<代码>$className=$this->className$className::model()->exists()。回答得很好,我错了!我使用$this->className作为变量,但它在'->'上给出了一个错误。把这个放在另一个变量中就行了<代码>$className=$this->className$className::model()->exists()。回答得很好,我错了!我使用$this->className作为变量,但它在'->'上给出了一个错误。把这个放在另一个变量中就行了<代码>$className=$this->className$className::model()->exists()。回答得很好,我错了!我使用$this->className作为变量,但它在'->'上给出了一个错误。把这个放在另一个变量中就行了<代码>$className=$this->className$className::model()->exists()。回答得不错。