note 78460 deleted from language.oop5.overloading by danbrown

From: Date: Mon, 20 Jun 2011 15:29:46 +0000
Subject: note 78460 deleted from language.oop5.overloading by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180773@lists.php.net to get a copy of this message
Note Submitter: egingell at sisna dot com ---- @ zachary dot craig at goebelmediagroup dot com I do something like that, too. My way might be even more clunky. <?php function sum() { $args = func_get_args(); if (!count($args)) { echo 'You have to supply something to the function.'; } elseif (count($args) == 1) { return $args[0]; } elseif (count($args) == 2) { if (is_numeric($args[0]) && is_numeric($args[1])) { return $args[0] + $args[1]; } else { return $args[0] . $args[1]; } } } ?> I like the "__call" method, though. You can "declare" multiple functions that don't pollute the namespace. Although, it might be better looking to use this instead of a series of if...elseif... statemenets. <?php class myClass { function __call($fName, $fArgs) { switch($fName) { case 'sum': if (count ($fArgs) === 1) { return $fArgs[0]; } else { $retVal = 0; foreach( $fArgs as $arg ) { $retVal += $arg; } return $retVal; } break; case 'other_method': ... default: die ('<div><b>Fetal Error:</b> unknown method ' . $fName . ' in ' . __CLASS__ . '.</div>'); } // end switch } } ?> Side note: if you don't force lower/upper case, you will have case sensitive method names. E.g. $myclass->sum() !== $myclass->Sum().

« previous php.notes (#180773) next »