note 39897 deleted from ref.overload by sobak

From: Date: Tue, 22 Nov 2016 03:49:06 +0000
Subject: note 39897 deleted from ref.overload by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207446@lists.php.net to get a copy of this message
Note Submitter: jw at jwscripts dot com ---- 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; ?>

« previous php.notes (#207446) next »