Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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/4/oop/2.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_Oop - Fatal编程技术网

Php 参数数目可变的静态创建者

Php 参数数目可变的静态创建者,php,oop,Php,Oop,我正在寻找一种最好的方法,在一个类族中的抽象级别上使用一个静态创建方法,该类族的构造函数中可以有数量可变的参数 例如,如果我知道所有扩展版本的Foo都有两个构造函数参数,我可以这样做: abstract class Foo { abstract public function __construct($arg1,$arg2); public static function Create(){ $args = func_get_args(); if(

我正在寻找一种最好的方法,在一个类族中的抽象级别上使用一个静态创建方法,该类族的构造函数中可以有数量可变的参数

例如,如果我知道所有扩展版本的Foo都有两个构造函数参数,我可以这样做:

abstract class Foo {
    abstract public function __construct($arg1,$arg2);
    public static function Create(){
        $args = func_get_args();
        if(count($agrs)!=2) throw new Exception('Invalid number of arguments');
        $classname = __CLASS__;
        return new $classname($args[0],$args[1]);
    }
}
但是,如果没有定义构造函数,参数的数量可能会有所不同,那么情况又如何呢

abstract class Foo {
    public static function Create(){
        $args = func_get_args();
        $classname = __CLASS__;
        return new $classname($args[0],$args[1],$args[2]); //errors if less than 3 arguments were passed
    }
}

class Foo_1 extends Foo {
  public function __construct($arg1){
      //some code...
  }
}

class Foo_2 extends Foo {
  public function __construct($arg1,$arg2,$arg3){
      //some code...
  }
}
我能想到的最好办法是测试这样传递的参数:

public static function Create(){
    $args = func_get_args();
    $classname = __CLASS__;
    switch count($args){
        case 0: return new $classname();
        case 1: return new $classname($args[0]);
        case 2: return new $classname($args[0],$args[1]);
        //etc....
    }
}

最简单的方法是使用反射系统

$reflector = new ReflectionClass($className);
$args = array('foo', 'bar');
// $instance will be an instance of $className
// $args should be an array containing the arguments to
// pass to the constructor
$instance = $reflector->newInstanceArgs($args);

最简单的方法是使用反射系统

$reflector = new ReflectionClass($className);
$args = array('foo', 'bar');
// $instance will be an instance of $className
// $args should be an array containing the arguments to
// pass to the constructor
$instance = $reflector->newInstanceArgs($args);

谢谢,我会查出来的。我忘了思考。谢谢。我会查出来的。我已经忘记了思考。