note 91347 added to language.oop5.overloading
| From: | jeshtan at gmail dot com | Date: | Sun, 07 Jun 2009 09:24:01 +0000 |
| Subject: | note 91347 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156429@lists.php.net to get a copy of this message | ||
what maciej at killer-robot dot net trying to means is that "overloading" refers to the
ability to declare multiple functions with the same name, which get called based on the number/type
of parameters.
For example, we has a Member class with 3 named getMember but have different number/type of
parameters.
class Member {
//get a member data from database by its id, in this case $memberid data type is integer
public function getMember($id) {}
//get a member data from database by its email, in this case $email data type is string
public function getMember($email) {}
//get a member data from database by its username and $password, both paramters data type are
string
public function getMember($username, $password) {}
}
The above codes are invalid in php but make sense in other real OOPL. The easy solution to overcome
this issue is giving each methods an unique name such as getMemberById etc.
Another way is declare only a getMember method without any predefined argument then checking the
data type and number or parameters inside the method itself. A quick example:
class Member {
public function getMember() {
$params = func_get_args();
if (count($params) == 1) {
if (is_int($params[0]) {
//code of get member by id here
} else if(is_string($params[0])) {
//code of get member by email here
}
} else if (count($params) == 2) {
//code of get member by username and password here
}
}
}
as everyone can see, the code is consists a lots of checking stuff and hard for managing it. Try
imagine one day you have to declare another getMember method with 2 parameter, 1 is string and
another is integer. However, it works as overloading should be.
----
Server IP: 66.207.199.35
Probable Submitter: 115.135.81.159
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/91347
Del: integrated -- https://master.php.net/note/delete/91347/integrated
Del: useless -- https://master.php.net/note/delete/91347/useless
Del: bad code -- https://master.php.net/note/delete/91347/bad+code
Del: spam -- https://master.php.net/note/delete/91347/spam
Del: non-english -- https://master.php.net/note/delete/91347/non-english
Del: in docs -- https://master.php.net/note/delete/91347/in+docs
Del: other reasons-- https://master.php.net/note/delete/91347
Reject -- https://master.php.net/note/reject/91347
Search -- https://master.php.net/manage/user-notes.php