20,359
社区成员
发帖
与我相关
我的任务
分享
class test{
public function __construct($host="127.0.0.1",$port="80")
{
echo 'host:'.$host."<br>";
echo 'port:'.$port;
}
}
$data['host']='192.168.1.1';
$data['port']='1212';
$str=implode(',',$data);
$obj=new test($str);
host:192.168.1.1,1212
port:80
class test{
public function __construct($host="127.0.0.1",$port="80")
{
echo 'host:'.$host."<br>";
echo 'port:'.$port;
}
}
$data['host']='192.168.1.1';
$data['port']='1212';
$ref = new ReflectionClass('test');
$obj = $ref->newInstanceArgs($data);
host:192.168.1.1
port:1212
<?php
class test{
public static function new_object($data = array()) {
switch(count($data)) {
case 2:
return new self($data['host'], $data['port']);
case 1:
return new self($data['host']);
default:
return new self();
}
}
public function __construct($host="127.0.0.1",$port="80")
{
echo 'host:'.$host."<br>";
echo 'port:'.$port;
}
}
$data['host']='192.168.1.1';
$data['port']='1212';
//$str=implode(',',$data);
//$obj=new test($str);
$obj = test::new_object($data);
host:192.168.1.1
port:1212