Home  >  Article  >  Backend Development  >  Using objects as arrays in PHP

Using objects as arrays in PHP

巴扎黑
巴扎黑Original
2016-12-01 09:57:162275browse

We understand that in JAVASCRIPT, the properties and methods of objects can be accessed using array mode. But usually this is not possible.

Why do you do this? This is because, in this way, objects can be manipulated more conveniently and we can define a class. Instead of defining a Key Value array. Naturally, if we have other methods, the simplest one is to force conversion into an array. However, this will lose the original methods in the object.

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

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

ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable {
/* 常量 */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* 方法 */
__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 )
}

Among them: Why can we use $obj['name'] to directly access $obj->name Woolen cloth? Mainly three of the above methods: offsetGet supports the $obj['name'] reading method

offsetSet supports the $obj['name'] writing method

But foreach is the method of ArrayAccess. Default implementation of functions Current etc.

                                                                                                                                                                                                       hello

                                       .

(

[name] => hello

[nick] => mockArray

)

Array
(
[name] => hello
[nick] => mockArray
)
Array
(
[name] = >
[ test1 age] => 21
)

It can be seen that you can use the array mode to access properties, but you cannot access methods (member functions).即 After mandatory conversion, it is an array object, and there is no member function.

                                                                                                                                                                                                                      in in ,         in Canada, in 201 in, in 2020, in the 2019-12-22 2018 0.01% OffsetGet OffsetSet ” Why? Because, if there are some very abnormal needs, it will definitely be useful. For example, we want to wrap three arrays into an object using references and access them as an array. At this time, these two functions must be rewritten. Of course, the corresponding functions in the ArrayAccess interface must also be rewritten.访 No matter how can you access the public attributes. If it is private, it cannot be accessed. Even if it is forced to an array, it is the same. But if you don't inherit ArrayObject, it's different. Once such a class is forced to be converted into an array, its privacy (private attributes) will be exposed.

But we can see that after the private attribute is converted into an array, the original attribute name is not retained. Instead, the form is used: a certain non-printable character + class name + non-printable character + attribute name. The ASCII value of this unprintable character has not been checked. If you are interested, you can check it out!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn