Does PHP support method overloading?
Does PHP support method overloading?
No, PHP does not natively support method overloading. Method overloading is a feature in object-oriented programming where a class can have multiple methods with the same name but different parameters. The appropriate method is called based on the number and type of arguments passed during the method invocation.
In PHP, if you define multiple methods with the same name in a class, only the last defined method will be used. All previous methods with the same name will be overwritten.
Here's an example to illustrate this:
class MyClass {
public function myMethod($arg1) {
echo "Method with one argument: $arg1";
}
public function myMethod($arg1, $arg2) {
echo "Method with two arguments: $arg1, $arg1";
}
}
$obj = new MyClass();
$obj->myMethod(10); // Output: Method with two arguments: 10, 10
In the above example, the second myMethod()
definition overwrites the first one, so only the method with two arguments is accessible.
However, PHP provides a way to a...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào