overloading methods in php 5... whats this crap?
Posted: Mon Jun 14, 2004 11:31 am
So I hear php5 supports overloading methods, and I think great. I really didn't think id have to learn too much, as ive overloaded methods in java and c++ and all it took was defining a method multiple times with different parameters. So I'm looking at the php documentation and Im trying to decypher their version of overloading and im thinking to myself, "What is this crap??"
Here are some examples of overloading the the php site
Example: __get() and __set()
Example: __call()
Is it me, or is it hard to follow what this code is supposed to do? I also fail to see how this is overloading.
feyd use
Here are some examples of overloading the the php site
Example: __get() and __set()
Code: Select all
<?php
class Setter {
public $n;
public $x = array("a" => 1, "b" => 2, "c" => 3);
function __get($nm) {
print "Getting [$nm]\n";
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
print "Nothing!\n";
}
}
function __set($nm, $val) {
print "Setting [$nm] to $val\n";
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
print "OK!\n";
} else {
print "Not OK!\n";
}
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>Code: Select all
<?php
class Caller {
private $x = array(1, 2, 3);
function __call($m, $a) {
print "Method $m called:\n";
var_dump($a);
return $this->x;
}
}
$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>feyd use
Code: Select all
tags for php code please.[/color]