Why no overloading in PHP5?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



(I've posted this to the PHP newsgroups, as well, but as many here might not
read them, I post here, as well. I hope that's not considered "overboard",
and if so, please let me know)

To round off my trilogy of "why"'s about PHP... :) If this subject have been
discussed before, I'd appreciate a pointer to it. I again haven't found it
in a search of the PHP groups.

The PHP manual mentions "overloading"
(http://www.php.net/manual/en/language.oop5.overloading.php), but it isn't
really overloading at all... Not in the sense it's used in other languages
supporting overloading (such as C++ and Java). As one of the
user-contributed notes on that page says, it would be more appropriate to
call it "dynamic methods and properties". What "overloading" in PHP is
about, is to be able to do $object-><name>(...), and it will call the
built-in function __call() with <name> and an array of the parameters.

Since "overloading" is used in this rather confusing sense (compared to
other languages) in PHP, it may be useful with a brief recap of how it works
in C++ and Java. In these languages, overloading means that you may have
several (member or non-member) functions with the same name, as long as
their signature is different (number and type of arguments). Translated to
PHP, this could look like this:

function f($a) {...}                // #1
function f($a, $b) {...}         // #2
function f($a, b$, $c) {...}   // #3
function f(Person $a) {...}  // # 4

f(1);           // Calls #1
f(1,"test"); // Calls #2
f(1,2,3);    // Calls #3
$obj=new Person();
f($obj);     // Calls #4

The last call would technically also match #1, but the one with type hint
(Person) might be considered "more specialised".

This issue has, like type hints for built-in types, been asked in a Zend Q &
A, such as this one: http://www.zend.com/expert_qa/qas.php?id=10&single=1

--- Start quote ---

public Object child() {
    return this.child;
}
public Object child(Object p_child) {
    this.child = p_child;
    return this.child();
}

So this is what you call function overloading? Questions: - Why is it called
function overloading? - Why won't it be supported in PHP? (important)

    It is called function overloading because you have two instances of the
same function name but they differ only by the function arguments. You
expect the right one to be called according to the arguments. It won't be
supported by PHP because it doesn't fit in with its dynamically typed value
paradigm and execution methodology. However, you may reach similar affects
by using optional function arguments for example:

public Object child(Object p_child=NULL) {
    if (p_child != NULL) {
        this.child = p_child;
    }
    return this.child;
}

--- End quote ---

Again, I don't find the answer satisfactory, but perhaps someone here can
convince me?

Even if PHP has loose/weak typing, then as for type hints for built-in
types, a value or variable has a specific type at any one time. At the very
least, one might provide overloading for functions taking arguments of
user-defined types (objects), in the same way as one provide optional static
typing in the form of type hints. I.e.:

function print(Person $p) {...}
function print(Something $s) {...}

Comments?

Regards,

Terje

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux