note 69440 added to language.oop5.overloading

From: Date: Wed, 06 Sep 2006 09:35:29 +0000
Subject: note 69440 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-116752@lists.php.net to get a copy of this message
Since many here probably wanted to do «real» overloading without having to think too much, here's a generic __call() function for those cases. Little example : <?php class OverloadedClass { public function __call($f, $p) { if (method_exists($this, $f.sizeof($p))) return call_user_func_array(array($this, $f.sizeof($p)), $p); // function does not exists~ throw new Exception('Tried to call unknown method '.get_class($this).'::'.$f); } function Param2($a, $b) { echo "Param2($a,$b)\n"; } function Param3($a, $b, $c) { echo "Param3($a,$b,$c)\n"; } } $o = new OverloadedClass(); $o->Param(4,5); $o->Param(4,5,6); $o->ParamX(4,5,6,7); ?> Will output : Param2(4,5) Param3(4,5,6) Fatal error: Uncaught exception 'Exception' with message 'Tried to call unknown method OverloadedClass::ParamX' in overload.php:7 Stack trace: #0 [internal function]: OverloadedClass->__call('ParamX', Array) #1 overload.php(22): OverloadedClass->ParamX(4, 5, 6, 7) #2 {main} thrown in overload.php on line 7 ---- Server IP: 195.221.21.36 Probable Submitter: 62.73.5.156 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/69440 Del: integrated -- https://master.php.net/note/delete/69440/integrated Del: useless -- https://master.php.net/note/delete/69440/useless Del: bad code -- https://master.php.net/note/delete/69440/bad+code Del: spam -- https://master.php.net/note/delete/69440/spam Del: non-english -- https://master.php.net/note/delete/69440/non-english Del: in docs -- https://master.php.net/note/delete/69440/in+docs Del: other reasons-- https://master.php.net/note/delete/69440 Reject -- https://master.php.net/note/reject/69440 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116752) next »