关于php 多个构造函数的问题 1.在一个文件中不可能同时存在两个相同名字的方法函数 2.如果在不同文件中 可以用命名空间的方式实现相同函数的调用(php版本要 5.3.0 以上)所以你的判断没有实在意义啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 php不能重载滴,把参数改成数组 在函数内区别吧 gettype -- 获取变量的类型construct 不是 php 类的构造函数__construct 才是!虽然 php 不支持重载,但楼主也并没有说是在一个类中的同名方法 自己在构造函数__construct中判断下参数类型,然后再走不同的分支就行了。不原生支持重载就模拟个嘛~ php 不向C一样不能支持函数重载,就像你说的通过func_num_arg函数然后判断参数来分支初始化嘛 可以在__construct中判断参数个数与类型。<?phpclass demo{ private $_args; public function __construct(){ $args_num = func_num_args(); if($args_num==2){ $this->_args = array( 'id' => func_get_arg(0), 'dname' => func_get_arg(1) ); }elseif($args_num==1 && is_array(func_get_arg(0))){ $this->_args = array( 'device'=>func_get_arg(0) ); }else{ exit('func param not match'); } } public function show(){ echo '<pre>'; print_r($this->_args); echo '</pre>'; }}// demo1$id = 1;$dname = 'fdipzone';$obj = new demo($id, $dname);$obj->show();// demo2$device = array('iOS','Android');$obj = new demo($device);$obj->show();?> php模拟登陆采集问题 如何根据读取的文字,改变其文字颜色? 菜鸟求助 PHP怎样得到一个数组中的指定键名的值? js菜单与flash冲突 php怎么循环读取该数据? 请问:一个这么简单的循环为什么会运行不出结果呢? php调试时的错误信息显示的问题 php与oicq 如何安装mysql? thinkphp调用数据库出错 discuz x3.1 curl发布帖子不成功,哪位高手能帮看看? 最近想看一看PHP内核方面的东西,还望前辈们教教
__construct 才是!虽然 php 不支持重载,但楼主也并没有说是在一个类中的同名方法
<?phpclass demo{ private $_args; public function __construct(){
$args_num = func_num_args();
if($args_num==2){
$this->_args = array(
'id' => func_get_arg(0),
'dname' => func_get_arg(1)
);
}elseif($args_num==1 && is_array(func_get_arg(0))){
$this->_args = array(
'device'=>func_get_arg(0)
);
}else{
exit('func param not match');
}
} public function show(){
echo '<pre>';
print_r($this->_args);
echo '</pre>';
}}// demo1
$id = 1;
$dname = 'fdipzone';
$obj = new demo($id, $dname);
$obj->show();// demo2
$device = array('iOS','Android');
$obj = new demo($device);
$obj->show();?>