Can I overload methods in PHP?

后端 未结 6 1916
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 20:01

Example:

I want to have two different constructors, and I don\'t want to use func_get_arg(), because then it\'s invisible what args are possible.

Is it legal

6条回答
  •  清歌不尽
    2021-01-06 20:09

    PHP has something that it calls "overloading" (via the __call magic method), but what really happens is the magic method __call is invoked when an inaccessible or non-existent method, rather like __get and __set let you "access" inaccessible/non-existent properties. You could use this to implement overloading of non-magic methods, but it's unwieldy.

    As formal parameters can be untyped (which is distinct from the argument values being untyped, since those are typed) and even function arity isn't strictly enforced (each function has a minimum number of arguments, but no theoretical maximum), you could also write the method body to handle different number and types of arguments.

提交回复
热议问题