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:

php Copy
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

What is the differences between $a != $b and $a !== $b ?

junior

What is the difference between single-quoted and double-quoted strings in PHP?

entry

What is the purpose of php.ini file?

Bình luận

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

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