Use objects as arrays in PHP

Source: Internet
Author: User

We understand that in JAVASCRIPT, object attributes and methods can be accessed in array mode. But it is generally impossible.

Why? This is because, through this method, objects can be operated more conveniently, And we can define a class. Instead of defining an array of Key values. Naturally, if we have other methods, one of the simplest is to forcibly convert them into arrays. However, this will lose the original method of the object.

However, ArrayObject in SPL can help us access attributes in array mode. But the method still cannot be implemented.

The ArrayObject class structure is as follows (some methods are added in php5, 1, or php5.2 ):

Php code
ArrayObject implements IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable {
/* Constant */
Const integer STD_PROP_LIST = 1;
Const integer ARRAY_AS_PROPS = 2;
/* Method */
_ Construct ([mixed $ input [, int $ flags [, string $ iterator_class])
Void append (mixed $ value)
Void asort (void)
Int count (void)
Array exchangeArray (mixed $ input)
Array getArrayCopy (void)
Int getFlags (void)
ArrayIterator getIterator (void)
Int getIteratorClass (void)
Void ksort (void)
Void natcasesort (void)
Void natsort (void)
Bool offsetExists (mixed $ index)
Mixed offsetGet (mixed $ index)
Void offsetSet (mixed $ index, mixed $ newval)
Void offsetUnset (mixed $ index)
Public void serialize (void)
Void setFlags (int $ flags)
Void setIteratorClass (string $ iterator_class)
Void uasort (callback $ cmp_function)
Void uksort (callback $ cmp_function)
Public void unserialize (string $ serialized)
}

ArrayObject implements IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable {
/* Constant */
Const integer STD_PROP_LIST = 1;
Const integer ARRAY_AS_PROPS = 2;
/* Method */
_ Construct ([mixed $ input [, int $ flags [, string $ iterator_class])
Void append (mixed $ value)
Void asort (void)
Int count (void)
Array exchangeArray (mixed $ input)
Array getArrayCopy (void)
Int getFlags (void)
ArrayIterator getIterator (void)
Int getIteratorClass (void)
Void ksort (void)
Void natcasesort (void)
Void natsort (void)
Bool offsetExists (mixed $ index)
Mixed offsetGet (mixed $ index)
Void offsetSet (mixed $ index, mixed $ newval)
Void offsetUnset (mixed $ index)
Public void serialize (void)
Void setFlags (int $ flags)
Void setIteratorClass (string $ iterator_class)
Void uasort (callback $ cmp_function)
Void uksort (callback $ cmp_function)
Public void unserialize (string $ serialized)
}

Here: Why can we use $ obj [name] to directly access $ obj-> name? The preceding three methods are used:

OffsetGet supports $ obj [name] Reading.

OffsetSet supports $ obj [name] writing.

However, foreach is the default implementation of this class for ArrayAccess functions such as Current.

Let's look at the example code:

Php code
Class test extends ArrayObject {
Public $ name;
Private $ age = 21;
Public function show (){
Print_r (get_object_vars ($ this ));
}
}
Class test1 {
Public $ name;
Private $ age = 21;
Public function show (){
Print_r (get_object_vars ($ this ));
}
}
$ Obj = new test ();
// Read and write attributes using Arrays
$ Obj [name] = hello;
$ Obj [nick] = mockArray;
Echo $ obj [nick], </br>;
Var_dump ($ obj [show]); // checks whether an access method is allowed:
Print_r ($ obj); // output object
$ Obj-> show (); // CALL THE METHOD
$ Arr = (array) $ obj; // forcibly convert to an array.
Print_r ($ arr );
// $ Arr-> show (); this row will fail because all the original methods are lost.
$ Obj1 = new test1 (); // create a common object
$ Arr1 = (array) $ obj1; // forcibly convert to an array.
Print_r ($ arr1); // completely exposed privacy

Class test extends ArrayObject {
Public $ name;
Private $ age = 21;
Public function show (){
Print_r (get_object_vars ($ this ));
}
}
Class test1 {
Public $ name;
Private $ age = 21;
Public function show (){
Print_r (get_object_vars ($ this ));
}
}
$ Obj = new test ();
// Read and write attributes using Arrays
$ Obj [name] = hello;
$ Obj [nick] = mockArray;
Echo $ obj [nick], </br>;
Var_dump ($ obj [show]); // checks whether an access method is allowed:
Print_r ($ obj); // output object
$ Obj-> show (); // CALL THE METHOD
$ Arr = (array) $ obj; // forcibly convert to an array.
Print_r ($ arr );
// $ Arr-> show (); this row will fail because all the original methods are lost.
$ Obj1 = new test1 (); // create a common object
$ Arr1 = (array) $ obj1; // forcibly convert to an array.
Print_r ($ arr1); // completely exposed privacy

This code will output:

MockArray </br> NULL
Test Object
(
[Name] => hello
[Nick] => mockArray
)
Array
(
[Name] => hello
[Nick] => mockArray
)
Array
(
[Name] => hello
[Nick] => mockArray
)
Array
(
[Name] =>
[Test1 age] => 21
)

We can see that the attribute can be accessed in array mode, but the method (member function) cannot be accessed ).

After forced conversion, it is an array object and no member function is available.

Of course, the offsetGet offsetSet methods can be further rewritten as needed. Why? This is because it is useful when there are abnormal demands. For example, we need to wrap the three arrays into an object as an array for access. At this time, we need to rewrite these two functions. Of course, you must also rewrite the corresponding functions in the ArrayAccess interface.

In addition, all accessible attributes are public attributes. If it is private, it cannot be accessed. The same applies even if it is forcibly converted to an array. However, if ArrayObject is not inherited, it is different. Once such a class is forcibly converted to an array, its privacy (Private Attribute) will be exposed.

However, we can see that the original property name is not retained after the private property is converted into an array. Instead, it uses the form of a non-printable character + class name + non-printable character + attribute name. The printable ASCII character is missing. If you are interested, check it!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.