note 114610 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:31:51 +0000 |
| Subject: | note 114610 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211908@lists.php.net to get a copy of this message | ||
Note Submitter: Nehuen
----
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');