PHP5 如何用__call 重载构造函数 http://bbs.17php.com/show_title.php?id=1443 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果连重载都没有哪还有脸说OO<?php//基类class Animal{ //基类的属性 public $name; //名字 //基类的构造函数 public function __construct( $name ) { $this->name = $name; }}//派生类class Person extends Animal //Person类继承了Animal类{ public $personSex; //性别 public $personAge; //年龄 //继承类的构造函数 function __construct( $personSex, $personAge ) { parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数 $this->personSex = $personSex; $this->personAge = $personAge; } function printPerson() { print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge ); }}//实例化Person对象$personObject = new Person( "male", "");//执行打印$personObject->printPerson(); //输出:heiyeluren is male,this year ?> php可以用模拟重载,比如使用call,在调用不存的方法时执行,比如class a{function __call($methodName,$methodArgs){$newMethodName=$methodName.count($methodArgs);//这时候可以用参数个数/类型等判断if (method_exists($this,$newMethodName)){call_user_func_array(array($this,$newMethodName),$methodArgs);}}function x1($arg1){echo $arg1;}//实际上执行另外两个不同名的方法,来模拟function x2($arg1,$arg2){echo $arg1.$arg2;}}$a=new a;$a->x(1);//本来是不可能的,但是call的时候判断了,call内容要自己写$a->x(1,2);call在实例化后调用,构造函数就不行了,那就修改构造函数class b{function __construct(){$newMethodName=__CLASS__.func_num_args();//这时候可以用参数个数/类型等判断$newMethodArgs=func_get_args();if (method_exists($this,$newMethodName)){call_user_func_array(array($this,$newMethodName),$newMethodArgs);}}function b1($arg1){echo $arg1;}//实际上执行另外两个不同名的方法,来模拟function b2($arg1,$arg2){echo $arg1.$arg2;}}$b=new b(1);$b=new b(1,2); windows 下重新编译 php 源码中自带的扩展问题 想成为一名出色的PHPer吗? 页面提交问题??? 急求~~请PHP高手指点一下netbeans的使用!! 大家帮助我看下,这个问题这么解决!谢谢!!! eclipse3.2和phpeclipse1.1.8的问题 怎么才能统计用户在登陆时输入10次错误 浏览器关闭后session怎么未消除? help me, 如何在UNIX下发送HTML格式邮件? linux下有没有方便一点的php编程工具?要不对linux还真的要失望 PHP6 require_once 的问题,在线等,急!!!!! PHP图片合成效果变很差该怎么解决
class Animal
{
//基类的属性
public $name; //名字 //基类的构造函数
public function __construct( $name )
{
$this->name = $name;
}
}//派生类
class Person extends Animal //Person类继承了Animal类
{
public $personSex; //性别
public $personAge; //年龄 //继承类的构造函数
function __construct( $personSex, $personAge )
{
parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数
$this->personSex = $personSex;
$this->personAge = $personAge;
} function printPerson()
{
print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
}
}//实例化Person对象
$personObject = new Person( "male", "");//执行打印
$personObject->printPerson(); //输出:heiyeluren is male,this year ?>
function __call($methodName,$methodArgs){
$newMethodName=$methodName.count($methodArgs);//这时候可以用参数个数/类型等判断
if (method_exists($this,$newMethodName)){
call_user_func_array(array($this,$newMethodName),$methodArgs);
}
}function x1($arg1){echo $arg1;}//实际上执行另外两个不同名的方法,来模拟
function x2($arg1,$arg2){echo $arg1.$arg2;}
}
$a=new a;
$a->x(1);//本来是不可能的,但是call的时候判断了,call内容要自己写
$a->x(1,2);
call在实例化后调用,构造函数就不行了,那就修改构造函数class b{
function __construct(){
$newMethodName=__CLASS__.func_num_args();//这时候可以用参数个数/类型等判断
$newMethodArgs=func_get_args();
if (method_exists($this,$newMethodName)){
call_user_func_array(array($this,$newMethodName),$newMethodArgs);
}
}function b1($arg1){echo $arg1;}//实际上执行另外两个不同名的方法,来模拟
function b2($arg1,$arg2){echo $arg1.$arg2;}
}$b=new b(1);
$b=new b(1,2);