How can an object be an array at the same time?

Source: Internet
Author: User
Tags spl
How can an object be an array at the same time? In simpleXML, the xml file is converted into an object. the root node is the object, and the access subnode is the member attribute of the object. See the following xml document:

 
 
  
   
    
How to Succeed?
   Zhang San
   
    
39.8
   
  
  
   
    
Characteristics of successful people
   Li Si
   
    
42.8
   
  
 


$ Xml_obj = simplexml_load_file ("books. xml "); $ books = $ xml_obj-> book; $ book = $ books [0]; // This is the first book. it is an object, the sub-nodes under him can be obtained by accessing his member method, but his attributes, $ lang = $ book ["lang"]; // obtain its attributes, lang. It is accessed as an array, but is it not an object?


What I don't understand is this. accessing node attributes is an array, but $ book is an object! How has it become an array?


Reply to discussion (solution)

Http://www.php.net/manual/zh/class.arrayaccess.php

[Book] => Array
(
[0] => SimpleXMLElement Object
(
[@ Attributes] => Array
(
[Lang] => Chinese
)

[Name] => How to Succeed?
[Author] => James
[Price] = & gt; 39.8
)

Extract the manual section and pay attention to the red section:
-------------------------------------------------------
The SimpleXMLElement class
(No version information available, might only be in SVN)
Introduction
Represents an element in an XML document.
Class abstract
SimpleXMLElement implements Traversable {
......
-----------------------------------------------------------------

Traversable is a class of SPL (currently the manual does not have a specific introduction, but it can be found in php.net)
Here is a short excerpt for you.

-----------------------------------------------------------------------------

Traversable Interface Reference
[Zend engine classes]
Interface to detect a class is traversable using foreach. More...

Detailed Description
Interface to detect a class is traversable using foreach. // This means the object can be iterated.

Since:
Python 5.0
Abstract base interface that cannot be implemented alone. Instead it must be implemented by either IteratorAggregate or Iterator.

Note:
Internal classes that implement this interface can be used in a foreach construct and do not need to implement IteratorAggregate or Iterator.
This is an engine internal interface which cannot be implemented in PHP scripts. Either IteratorAggregate or Iterator must be used instead.


Definition at line 509 of file spl. php.


--------------------------------------------------------------------------------
The documentation for this interface was generated from the following file:
Spl. php

[Book] => Array
(
[0] => SimpleXMLElement Object
(
[@ Attributes] => Array
(
[Lang] => Chinese
)

[Name] => How to Succeed?
[Author] => James
[Price] = & gt; 39.8
)


[@attributes] => Array


What does @ mean here? This @ symbol is not displayed for the authentic array var_dump.

Attribute array of SimpleXMLElement object
The operation is generally performed by traversing the array returned by the attributes method.
You can write like that.

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.