note 79864 added to language.oop5.overloading
| From: | matthijs at yourmediafactory dot com | Date: | Sun, 16 Dec 2007 19:09:20 +0000 |
| Subject: | note 79864 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133716@lists.php.net to get a copy of this message | ||
While PHP does not support true overloading natively, I have to disagree with those that state this
can't be achieved trough __call.
Yes, it's not pretty but it is definately possible to overload a member based on the type of
its argument. An example:
<?php
class A {
public function __call ($member, $arguments) {
if(is_object($arguments[0]))
$member = $member . 'Object';
if(is_array($arguments[0]))
$member = $member . 'Array';
$this -> $member($arguments);
}
private function testArray () {
echo "Array.";
}
private function testObject () {
echo "Object.";
}
}
class B {
}
$class = new A;
$class -> test(array()); // echo's 'Array.'
$class -> test(new B); // echo's 'Object.'
?>
Of course, the use of this is questionable (I have never needed it myself, but then again, I only
have a very minimalistic C++ & JAVA background). However, using this general principle and
optionally building forth on other suggestions a 'form' of overloading is definately
possible, provided you have some strict naming conventions in your functions.
It would of course become a LOT easier once PHP'd let you declare the same member several times
but with different arguments, since if you combine that with the reflection class 'real'
overloading comes into the grasp of a good OO programmer. Lets keep our fingers crossed!
----
Server IP: 213.249.64.162
Probable Submitter: 82.73.191.141
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/79864
Del: integrated -- https://master.php.net/note/delete/79864/integrated
Del: useless -- https://master.php.net/note/delete/79864/useless
Del: bad code -- https://master.php.net/note/delete/79864/bad+code
Del: spam -- https://master.php.net/note/delete/79864/spam
Del: non-english -- https://master.php.net/note/delete/79864/non-english
Del: in docs -- https://master.php.net/note/delete/79864/in+docs
Del: other reasons-- https://master.php.net/note/delete/79864
Reject -- https://master.php.net/note/reject/79864
Search -- https://master.php.net/manage/user-notes.php