note 91347 deleted from language.oop5.overloading by danbrown

From: Date: Mon, 08 Jun 2009 12:38:22 +0000
Subject: note 91347 deleted from language.oop5.overloading by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156452@lists.php.net to get a copy of this message
Note Submitter: jeshtan at gmail dot com ---- 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.

« previous php.notes (#156452) next »