note 111918 added to language.oop5.overloading
| From: | hishamdalal at gmail dot com | Date: | Thu, 11 Apr 2013 10:11:02 +0000 |
| Subject: | note 111918 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194451@lists.php.net to get a copy of this message | ||
Real Overload
class overloadable
{
protected $data = array();
protected $fname = null;
protected $fargs = array();
protected $args_count = 0;
protected $obj = null;
//--------------------------------------------------//
function set($obj, $fname, $args){
$n = '';
$type = $this->getType($args);
$n = ("\$o = new ".get_class($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\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($this, $name, $args);
}
//--------------------------------------------------//
}
###########
class test2 extends overloadable {
function foo_(){
echo 'just foo';
}
function foo_S($s){
echo "foo and string $s";
}
function foo_SS($s1, $s2){
echo "foo and tow stringa $s1, $s2";
}
function foo_SN($s, $n){
echo "foo string and number $s, $n";
}
function foo_A($ary){
print_r($ary);
}
function foo_AA($ary1, $ary2){
if(is_array($ary1) && is_array($ary2)){
echo "tow arrays";
}else{echo 0;}
}
function foo_O($obj){
print_r($obj);
}
function hi(){
echo "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() );
----
Server IP: 69.147.83.199
Probable Submitter: 31.31.184.9
----
Manual Page -- http://php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/111918
Del: integrated -- https://master.php.net/note/delete/111918/integrated
Del: useless -- https://master.php.net/note/delete/111918/useless
Del: bad code -- https://master.php.net/note/delete/111918/bad+code
Del: spam -- https://master.php.net/note/delete/111918/spam
Del: non-english -- https://master.php.net/note/delete/111918/non-english
Del: in docs -- https://master.php.net/note/delete/111918/in+docs
Del: other reasons-- https://master.php.net/note/delete/111918
Reject -- https://master.php.net/note/reject/111918
Search -- https://master.php.net/manage/user-notes.php