<?php
/*******************************
* author : hishamdalal@gmail.com
* version : 3.1
* date : 2013-04-11
*****************************/
class overloadable
{
protected $fname = null;
protected $fargs = array();
//--------------------------------------------------//
function set($obj, $fname, $args){
$n = '';
$type = $this->getType($args);
$n = "\$o = new $obj();\n";
$n .= "if(method_exists(\$o, '$fname"."_$type')){\n";
$n .= "\t\$r = \$o->$fname"."_$type(". $this->getArgsName($args) .");\n";
$n .= "}else{\n\t\$r = null;\n";
$n .= "\ttrigger_error('function ".$fname."_".$type." is not exist!');\n}";
eval("\$r = $n;");
return $r;
}
//--------------------------------------------------//
function getType($args) {
$argType = array();
foreach($args as $i=>$val) {
$argType[$i][] = $this->getSuffix($val, $i) ;
}
$s = '';
if(is_array($argType)){
foreach($argType as $type){
$s .= implode('', $type);
}
return $s;
}
return implode('', $argType);
}
//--------------------------------------------------//
function getSuffix($byValarg, $i) {
if( is_numeric($byValarg) ) {
$type = 'N';
$this->fargs['N'.$i] = $byValarg;
} elseif( is_array($byValarg) ) {
$type = 'A';
$this->fargs['A'.$i] = $byValarg;
} elseif( is_object($byValarg) ) {
$type = 'O';
$this->fargs['O'.$i] = $byValarg;
} elseif( is_resource($byValarg) ) {
$type = 'R';
$this->fargs['R'.$i] = $byValarg;
} else {
$type = 'S';
$this->fargs['S'.$i] = $byValarg;
}
return $type;
}
//--------------------------------------------------//
function getArgsName($args){
$r = array();
$ary = array_keys($this->fargs);
foreach( $ary as $k=>$v){
$r[]='$this->fargs["'.$v.'"]';
}
return implode(", ", $r);
}
//--------------------------------------------------//
function __call($name, $args){
$this->fargs = array();
return $this->set(get_class($this), $name, $args);
}
//--------------------------------------------------//
}
class test2 extends overloadable {
function foo_(){
echo 'foo - no args';
}
function foo_S($s){
echo "foo - one string $s";
}
function foo_SS($s1, $s2){
echo "foo - tow strings $s1, $s2";
}
function foo_SN($s, $n){
echo "foo - string and number $s, $n";
}
function foo_SNN($s, $n){
echo "foo - string and two numbers $s, $n, $n";
}
function foo_A($ary){
print_r($ary);
}
function foo_AA($ary1, $ary2){
if(is_array($ary1) && is_array($ary2)){
echo "foo - tow arrays";
}else{echo 0;}
}
function foo_O($obj){
echo "foo - ";
print_r($obj);
}
function hi(){
echo "hi - welcome!";
}
}
echo '<pre>';
$t = new test2();
echo '<br />foo_: ';
print_r( $t->foo() );
echo '<br />foo_s: ';
print_r( $t->foo('a') );
echo '<br />foo_ss: ';
print_r( $t->foo('a', 'b') );
echo '<br />foo_sn: ';
print_r( $t->foo('a', 2) );
echo '<br />foo_snn: ';
print_r( $t->foo('s', 2, 9) );
echo '<br />foo_a: ';
print_r( $t->foo(array(4,5,6,7)) );
echo '<br />foo_aa: ';
print_r( $t->foo( array(5,6,7), array(8,9,10) ) );
echo '<br />foo_o: ';
print_r( $t->foo($t) );
echo '<br />hi: ';
print_r( $t->hi() );
?>
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- <pre><br />foo_: foo - no args<br />foo_s: foo - one string a<br />foo_ss: foo - tow strings a, b<br />foo_sn: foo - string and number a, 2<br />foo_snn: foo - string and two numbers s, 2, 2<br />foo_a: Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
)
<br />foo_aa: foo - tow arrays<br />foo_o: foo - test2 Object
(
[fname:protected] =>
[fargs:protected] => Array
(
[O0] => test2 Object
*RECURSION*
)
)
<br />hi: hi - welcome!
preferences:
270.58 ms | 405 KiB | 407 Q