note 39897 added to ref.overload
| From: | jw at jwscripts dot com | Date: | Sat, 14 Feb 2004 16:10:48 +0000 |
| Subject: | note 39897 added to ref.overload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65143@lists.php.net to get a copy of this message | ||
The following backwards compatible code demonstrates the differences between the PHP version 4 and 5
implementation of overloading:
<?
class Foo {
// The properties array
var $array = array('a' => 1, 'b' => 2, 'c' => 4);
// Getter
function __get($n, $val = "") {
if (phpversion() >= 5) {
return $this->array[$n];
} else {
$val = $this->array[$n];
return true;
}
}
// Setter
function __set($n, $val) {
$this->array[$n] = $val;
if (phpversion() < 5) return true;
}
// Caller, applied when $function isn't defined
function __call($function, $arguments) {
// Constructor called in PHP version < 5
if ($function != __CLASS__) {
$this->$arguments[0] = $arguments[1];
}
if (phpversion() < 5) return true;
}
}
// Call the overload() function when appropriate
if (function_exists("overload") && phpversion() < 5) {
overload("Foo");
}
// Create the object instance
$foo = new Foo;
// Adjust the value of $foo->array['c'] through
// method overloading
$foo->set_array('c', 3);
// Adjust the value of $foo->array['c'] through
// property overloading
$foo->c = 3;
// Print the new correct value of $foo->array['c']
echo 'The value of $foo->array["c"] is: ', $foo->c;
?>
----
Manual Page -- http://www.php.net/manual/en/ref.overload.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39897
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39897&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39897&report=yes
Search -- http://master.php.net/manage/user-notes.php