PHP function overloading

前端 未结 10 1010
北恋
北恋 2020-11-22 17:24

Coming from C++ background ;)
How can I overload PHP functions?

One function definition if there are any arguments, and another if there are no arguments? Is it

10条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 17:51

    It may be hackish to some, but I learned this way from how Cakephp does some functions and have adapted it because I like the flexibility it creates

    The idea is you have different type of arguments, arrays, objects etc, then you detect what you were passed and go from there

    function($arg1, $lastname) {
        if(is_array($arg1)){
            $lastname = $arg1['lastname'];
            $firstname = $arg1['firstname'];
        } else {
            $firstname = $arg1;
        }
        ...
    }
    

提交回复
热议问题