Câu hỏi phỏng vấn PHP
Câu hỏi

Does PHP support method overloading?

Câu trả lời

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

middle

Gợi ý câu hỏi phỏng vấn

junior

Can you extend a Final defined class?

middle

What is autoloading classes in PHP?

senior

Store an array as JSON or as a PHP serialized array?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào