Use objects as arrays in PHP

Source: Internet
Author: User
Use the object as an array in PHP ??????? 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 a KeyValue array. Naturally, if we have other methods, one of the simplest is to forcibly convert them into numbers and use objects as arrays in PHP.

??????? 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 ):

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:

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/write attributes using arrays $ obj ['name'] = 'hello'; $ obj ['Nick '] = 'mockarray'; echo $ obj ['Nick '],'
'; Var_dump ($ obj ['show']); // check whether the access method is available: 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 the object to an array. Print_r ($ arr1); // completely exposed privacy

?

???????? This code will output:

MockArray
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 offsetGet? The offsetSet method 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.