note 69440 deleted from language.oop5.overloading by cmb

From: Date: Sun, 03 Mar 2019 11:38:07 +0000
Subject: note 69440 deleted from language.oop5.overloading by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210311@lists.php.net to get a copy of this message
Note Submitter: MagicalTux at ooKoo dot org ---- 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

« previous php.notes (#210311) next »