PHP constructors support different number of parameter methods
Principle: Use Func_num_args to get the number of parameters in __construct, and then perform different calls based on the number of parameters. Parameter values are obtained using the Func_get_arg () method.
Demo
<?php class demo{Private $_args; Public Function __construct () {$args _num = Func_num_args ();//Get the number of arguments//determine the number of parameters and type 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 (); ?>
Demo Output after execution:
Array
(
[id] => 1
[dname] => fdipzone
)
array
([
device] => array
([
0] => IOS
[1] => Android
)
Author: csdn blog proud Snow star Maple
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/