dougu2036 2013-07-23 19:53
浏览 42
已采纳

PHP是否具有C#,JAVA,C ++等函数重载功能[重复]

This question already has an answer here:

During my programming life in PHP, whenever I create function in PHP with same name, but different parameters this was cause error. Because of that I'm wondering did PHP has any kind of function overloading feature, and if it have I would appreciated to show me an example.

Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dql123000 2013-07-23 19:56
    关注

    Simply put: no. In PHP, a method signature doesn't include it's parameter set, only it's name. Therefore, two methods with the same name but different parameters are actually considered equal (and thus an error results).

    PHP does have a different process which it calls method overloading, but it's a different approach to the problem. In PHP, overloading is a means by which methods and properties can be dynamically created on an object at runtime. An example using the __call method follows.

    The __call method of a class will be invoked when there is no method matching the method name that was called inside of the class. It will receive the method name, and an array of the arguments.

    class OverloadTest {
        public function __call($method, $arguments) {
            if ($method == 'overloadedMethodName') {
                switch (count($arguments)) {
                    case 0:
                        return $this->overloadedMethodNoArguments();
                        break;
                    case 1:
                        return $this->overloadedMethodOneArgument($arguments[0]);
                        break;
                    case 2:
                        return $this->overloadedMethodTwoArguments($arguments[0], $arguments[1]);
                        break;
                }
            }
        }
    
        protected function overloadedMethodNoArguments() { print "zero"; }
        protected function overloadedMethodOneArgument($one) { print "one"; }
        protected function overloadedMethodTwoArguments($one, $two) { print "two"; }
    }
    
    $test = new OverloadTest();
    $test->overloadedMethodName();
    $test->overloadedMethodName(1);
    $test->overloadedMethodName(1, 2);
    

    Alternatively, you can provide a function with default arguments which will effectively allow for syntax that looks like overloading. Such as:

    function testFunction($one, $two = null, $three = null) {
    
    }
    
    testFunction(1);
    testFunction(1, 2);
    testFunction(1, 2, 3);
    

    And, finally, as for the third method, you can of course always access the arguments as an array within the function itself

    function variableFunction() {
        $arguments = func_get_args();
    
        switch (count($arguments)) {
           // ...
        }
    }
    
    variableFunction();
    variableFunction(1, 2, 3, 4, 5, 6, 7, 8, 9);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路