[PHP] object's properties

xiaoxiao2021-03-06  66

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]

/ ** * Define ObjectProperties Item * * @copyright Copyright (c) 2004 DUALFACE.com * @Author Liao Yue Lei * @version $ ID: ObjectProperties.class.php, v 1.1 2004/09/26 17:09:27 DUALFACE EXP $ * @Package Pfc * @since 3.0 * /

// {{{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 * @package pfc * @version 1.0 * / class Objectproperties {/ ** * Save all the name and access rules for all available properties * * @access private * @since 1.0 * @var Array * / var $ _properties; / ** * Save attribute value * * @access private * @since 1.0 * @var array * / var $ _properties_value; / ** * Constructor * * @Param Array $ Properties Determine available properties its access rules * * @return ObjectProperties * * @access public * @since 1.0 * / function ObjectProperties ($ properties, $ value) {$ this -> _ properties = $ properties; $ new_value = array (); foreach ($ properties AS $ prop_name => $ prop_rule) {if ($ value [$ prop_name])) {$ new_value [$ prop_name] = $ value [$ prop_name];} else {$ new_value [$ prop_name] = null;}} $ this -> _ Properties_Value = $ new_value;}

/ ** * 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]

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.

转载请注明原文地址:https://www.9cbs.com/read-110365.html

New Post(0)