来自c++背景;) 如何重载PHP函数?
一个函数定义如果有参数,另一个函数定义如果没有参数? 这在PHP中可行吗?或者我应该使用if else来检查是否有任何参数从$_GET和POST传递??把它们联系起来?
来自c++背景;) 如何重载PHP函数?
一个函数定义如果有参数,另一个函数定义如果没有参数? 这在PHP中可行吗?或者我应该使用if else来检查是否有任何参数从$_GET和POST传递??把它们联系起来?
当前回答
<?php
class abs
{
public function volume($arg1=null, $arg2=null, $arg3=null)
{
if($arg1 == null && $arg2 == null && $arg3 == null)
{
echo "function has no arguments. <br>";
}
else if($arg1 != null && $arg2 != null && $arg3 != null)
{
$volume=$arg1*$arg2*$arg3;
echo "volume of a cuboid ".$volume ."<br>";
}
else if($arg1 != null && $arg2 != null)
{
$area=$arg1*$arg2;
echo "area of square = " .$area ."<br>";
}
else if($arg1 != null)
{
$volume=$arg1*$arg1*$arg1;
echo "volume of a cube = ".$volume ."<br>";
}
}
}
$obj=new abs();
echo "For no arguments. <br>";
$obj->volume();
echo "For one arguments. <br>";
$obj->volume(3);
echo "For two arguments. <br>";
$obj->volume(3,4);
echo "For three arguments. <br>";
$obj->volume(3,4,5);
?>
其他回答
在PHP 5.6中,你可以使用splat操作符…作为最后一个参数,去掉func_get_args()和func_num_args():
function example(...$args)
{
count($args); // Equivalent to func_num_args()
}
example(1, 2);
example(1, 2, 3, 4, 5, 6, 7);
你也可以用它来解压缩参数:
$args[] = 1;
$args[] = 2;
$args[] = 3;
example(...$args);
等价于:
example(1, 2, 3);
PHP不支持传统的方法重载,但是有一种方法可以实现你想要的,那就是使用__call魔法方法:
class MyClass {
public function __call($name, $args) {
switch ($name) {
case 'funcOne':
switch (count($args)) {
case 1:
return call_user_func_array(array($this, 'funcOneWithOneArg'), $args);
case 3:
return call_user_func_array(array($this, 'funcOneWithThreeArgs'), $args);
}
case 'anotherFunc':
switch (count($args)) {
case 0:
return $this->anotherFuncWithNoArgs();
case 5:
return call_user_func_array(array($this, 'anotherFuncWithMoreArgs'), $args);
}
}
}
protected function funcOneWithOneArg($a) {
}
protected function funcOneWithThreeArgs($a, $b, $c) {
}
protected function anotherFuncWithNoArgs() {
}
protected function anotherFuncWithMoreArgs($a, $b, $c, $d, $e) {
}
}
对某些人来说,这可能有点棘手,但我从Cakephp处理某些函数的方式中学到了这种方法,并对其进行了调整,因为我喜欢它所创造的灵活性
你有不同类型的参数,数组,对象等等,然后你检测你传递了什么,然后从那里开始
function($arg1, $lastname) {
if(is_array($arg1)){
$lastname = $arg1['lastname'];
$firstname = $arg1['firstname'];
} else {
$firstname = $arg1;
}
...
}
要重载一个函数,只需在默认情况下将参数传递为空,
class ParentClass
{
function mymethod($arg1 = null, $arg2 = null, $arg3 = null)
{
if( $arg1 == null && $arg2 == null && $arg3 == null ){
return 'function has got zero parameters <br />';
}
else
{
$str = '';
if( $arg1 != null )
$str .= "arg1 = ".$arg1." <br />";
if( $arg2 != null )
$str .= "arg2 = ".$arg2." <br />";
if( $arg3 != null )
$str .= "arg3 = ".$arg3." <br />";
return $str;
}
}
}
// and call it in order given below ...
$obj = new ParentClass;
echo '<br />$obj->mymethod()<br />';
echo $obj->mymethod();
echo '<br />$obj->mymethod(null,"test") <br />';
echo $obj->mymethod(null,'test');
echo '<br /> $obj->mymethod("test","test","test")<br />';
echo $obj->mymethod('test','test','test');
不能重载PHP函数。函数签名仅基于它们的名称,不包括参数列表,因此不能有两个同名的函数。PHP中的类方法重载不同于许多其他语言。PHP使用了相同的词,但它描述了不同的模式。
但是,您可以声明一个可变参数函数,该函数接受可变数量的参数。您可以使用func_num_args()和func_get_arg()来获取传递的参数,并正常使用它们。
例如:
function myFunc() {
for ($i = 0; $i < func_num_args(); $i++) {
printf("Argument %d: %s\n", $i, func_get_arg($i));
}
}
/*
Argument 0: a
Argument 1: 2
Argument 2: 3.5
*/
myFunc('a', 2, 3.5);