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:7
Avg. Score:3.3 ± 0.7
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC