note 114610 added to language.oop5.overloading

From: Date: Wed, 12 Mar 2014 05:43:37 +0000
Subject: note 114610 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-198866@lists.php.net to get a copy of this message
Overloading other classical typed languages ​​can be emulated in a simple way: <?php class overload { protected function overloaded($method, $params, $type = false, $instance = false) { $method .= count($params); if($type && $params) { foreach($params as $param) $method .= '_'.(gettype($param) == 'object' ? ($instance ? get_class($param) : 'object') : gettype($param)); } if(method_exists($this, $method)) call_user_func_array(array($this,$method),$params); } } class test_overload extends overload { public function test_function() { $this->overloaded('test', func_get_args(), true); } protected function test0() { echo('<br>Hello, I did not pass any parameters.'); } protected function test1_string($string) { echo('<br>Hello, I spent this string: '.$string); } protected function test1_integer($integer) { echo('<br>Hello, I spent this integer: '.$integer); } protected function test2_integer_string($integer, $string) { echo('<br>Hello, I spent this integer: '.$integer.' and this string: '.$string); } protected function test2_string_integer($string, $integer) { echo('<br>Hello, I spent this string: '.$integer.' and this integer: '.$string); } } $test = new test_overload(); $test->test_function(); $test->test_function('test'); $test->test_function(5); $test->test_function(5, 'test'); $test->test_function('test', 5); $test->test_function('2'); ---- Server IP: 72.52.91.14 Probable Submitter: 190.7.251.125 ---- Manual Page -- http://php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/114610 Del: integrated -- https://master.php.net/note/delete/114610/integrated Del: useless -- https://master.php.net/note/delete/114610/useless Del: bad code -- https://master.php.net/note/delete/114610/bad+code Del: spam -- https://master.php.net/note/delete/114610/spam Del: non-english -- https://master.php.net/note/delete/114610/non-english Del: in docs -- https://master.php.net/note/delete/114610/in+docs Del: other reasons-- https://master.php.net/note/delete/114610 Reject -- https://master.php.net/note/reject/114610 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198866) next »