php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40388 Subclassed SimpleXMLElements with differently-subclassed child nodes
Submitted: 2007-02-07 15:00 UTC Modified: 2011-02-21 21:13 UTC
Votes:5
Avg. Score:3.4 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:2 (50.0%)
From: jazzslider at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.2.0 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jazzslider at gmail dot com
New email:
PHP Version: OS:

 

 [2007-02-07 15:00 UTC] jazzslider at gmail dot com
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-21 21:13 UTC] jani@php.net
-Package: Feature/Change Request +Package: SimpleXML related
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 09:01:27 2019 UTC