In PHP4, the custom object is not attribute. We either use a field to replace the properties, but cannot control whether the user can modify the value of these fields. Code: [Code] Class Test {var $ value; function test () {$ this-> value = 100;}}
$ T = new test (); Echo $ T-> Value; $ t-> value = 200; // In fact, we don't want the user to modify this field value at all. This field should be read-only. [/ Code] If the replacement attribute is called with a function, the "read-only properties" can be implemented, but the corresponding read function and write functions are added to the readable properties.
Code:
[code] class test2 {var $ _VALUE; function test2 () {$ this -> _ value = 100;} function getValue () {RETURN $ this -> _ value;} function setValue ($ new_value) {$ this -> _ value = $ new_value;}}
$ T = new test2 (); echo $ t-> getValue (); $ t-> setvalue (200); [/ code]
This is always not intuitive, and the code is not easy to maintain.
So I designed an ObjectProperties type, tried to simulate object properties.
[code] php // --------------------------------------- -------------------------------- // | This file is part of the PFC project. | // | CopyRight (C) 2004 Liao Yue. | // | / / | To view full copyright information and license information, check the copyright file included in the source code, | // | or access http://www.dualface.com/ to get details. | // ---------------------------------------------- -----------------------------
/ ** * Define ObjectProperties Item * * @copyright Copyright (c) 2004 DUALFACE.com * @Author Liao Yue Lei
// {{{constants / ** # @ * Property Access Rule * // ** * Read-only Properties * / Define ('Prop_Readonly', 1); / ** * Readable Options * / Define (' Prop_readwrite ', 2); / ** # @ - * ///}}} / ** * ObjectProperties Type Difference Define Objects with Access Rule Control Properties * * All ObjectProperties inheritance must be the final file Execute OVERLOAD (inherited name), otherwise you will not work properly. * @Author Liao Yueli
/ ** * callback function, used to obtain attribute value * @Param string $ prop_name property name * @Param mixed $ prop_value Save Property * * @Return Boolean Indicates whether it is successful * @access public * @since 1.0 * / function __get ($ prop_name, & $ prop_value) {if (isset ($ this -> _ properties [$ prop_name])) {$ prop_value = $ this -> _ properties_value [$ prop_name]; return true;} else {return false;} }
/ ** * Tune function, used to set the attribute value * * @Param string $ prop_name property name * @Param mixed $ prop_value attribute value * * @Return Boolean Indicates whether it is success * * @access public * @since 1.0 * / function __set ($ prop_name, $ prop_value) {if (isset ($ this -> _ properties [$ prop_name]) && $ this -> _ properties [$ prop_name] == PROP_READWRITE) {$ this -> _ properties_value [$ prop_name] = $ prop_value Return true;}}}}}}}} Overload ('ObjectProperties');
?> [/ code]
Such an effect can be achieved using ObjectProperties above:
Code: [code] class test3 extends ObjectProperties {function test3 () {$ properties = array ( "Value" => PROP_READONLY); $ value = array ( "Value" => 100); parent :: ObjectProperties ($ properties, $ Value);}}
OVERLOAD ("TEST3");
$ T = new test3 (); Echo $ T-> Value; $ t-> value = 200; // will result in errors [/ code]
All this looks beautiful!
Unfortunately, this approach has several serious defects: 1. Due to the attribute of the overload mechanism of OverLoad (), it is actually called __get () or in the case of accessing the object's properties. __set () function. If you do a lot of such attribute access operations, efficiency is no doubtful than using a member variable.
2. Perhaps there are some defects in the design of PHP4 overload (). When we try to set an OverLoad (That is, the PHP4 will report an error when the [reference] assignment to another OverLoad () function is invited to another OverLoad object. [code] PHP
Class test3 {function __get () {...}; // This is a simplified code function __set () {...};
Overload ('Test3');
Class test4 {function __get () {...}; function __set () {...}; var $ _X; function doomething (& $ obj) {$ this -> _ x = & $ obj; // here will report an error here If you remove it, you can run}}
Overload ('Test4');
?> [/ code]
3, in addition, if OverLoad () is applied in multiple inherits, it will cause a PHP running environment error.
It seems that this way can only be used as a test, and the actual application is still not. As for PHP5, there will be no two problems behind, I have not tested it.