|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-02-21 21:13 UTC] jani@php.net
-Package: Feature/Change Request
+Package: SimpleXML related
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 20:00:01 2025 UTC |
Description: ------------ I often use SimpleXML to provide a hierarchical organizational view of associated database records. For reference I have included some sample XML in the source code field. The Page element represents a record from a 'pages' table in the database; likewise, each Comment element represents an associated record in the 'comments' table. Sometimes, different types of data in my application require extra extensions to the SimpleXMLElement. This helps me get extra metadata or convenience features related to each content type. So, the Page object might need to be of class "PageInstance", which is itself a subclass of SimpleXMLElement. However, I'd like it if the associated Comment records contained within the PageInstance object could themselves be "CommentInstance" objects (another subclass of SimpleXMLElement). Reproduce code: --------------- <?php class PageInstance extends SimpleXMLElement { } class CommentInstance extends SimpleXMLElement { } $xml = '<Page><id>7</id><title>A Great Page</title><body>This is the body text of a great page.</body><Comment><page_id>7</page_id><user_id>12</user_id><body>Great job making this page!</body></Comment><Comment><page_id>7</page_id><user_id>15</user_id><body>I think this page is overrated.</body></Comment></Page>'; $sxe = simplexml_load_string($xml, 'PageInstance'); // Additional code making it possible to assign different // subclasses to children of $sxe (this is not currently // possible) echo get_class($sxe->Comment); ?> Expected result: ---------------- (Note that the following result is desired, but given the way SimpleXML currently works, it is not expected) ---------------------------------------------- CommentInstance Actual result: -------------- PageInstance