php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29092 Object Upcasting Occurs upon itterating children
Submitted: 2004-07-11 14:28 UTC Modified: 2004-07-21 12:23 UTC
From: Jason at hybd dot net Assigned:
Status: Not a bug Package: *XML functions
PHP Version: 5.0.0RC3 OS: Windows 2003 Enterprise Server
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
9 - 4 = ?
Subscribe to this entry?

 
 [2004-07-11 14:28 UTC] Jason at hybd dot net
Description:
------------
When using the itterator DomNode::childNodes to cycle through nodes objects that have been extended though PHP (In this case, TplTagDefault), the behaviour becomes inconsistent.

As you can see from the output, the object returned is sometimes a TplTagDefault but other times it's a DOM* object. Generally the last item in the DomNodeList is of the correct type.

I have posted this on a couple of forums/IRC support places and they all seem to agree it's a bug (though few people seem to have DOM/XML understanding). If this is not a bug, please could someone explain how I prevent objects from lossing their inheritance (i.e. prevent upcasting) when cycling through them.

-- Jay

Reproduce code:
---------------
<?php

class TplCompileTree extends DomDocument {}
class TplTagDefault extends DomElement {}

$docRoot = &new TplCompileTree();
$newNode = &new TplTagDefault('Root');
$docRoot->appendChild($newNode);
for($i = 0; $i < 3; $i++) {
    $newNode->appendChild(new TplTagDefault('Kiddy'.$i));
}
foreach($docRoot->childNodes AS $child) {
    var_dump($child);
    foreach($child->childNodes as $kids) {
        var_dump($kids);
    }
}
echo $docRoot->saveXML();

?> 

Expected result:
----------------
object(TplTagDefault)#13 (0) {
}
object(TplTagDefault)#18 (0) {
}
object(TplTagDefault)#20 (0) {
}
object(TplTagDefault)#14 (0) {
}
<?xml version="1.0"?>
<Root><Kiddy0/><Kiddy1/><Kiddy2/></Root>



Actual result:
--------------
object(TplTagDefault)#13 (0) {
}
object(DOMElement)#18 (0) {
}
object(DOMElement)#20 (0) {
}
object(TplTagDefault)#14 (0) {
}
<?xml version="1.0"?>
<Root><Kiddy0/><Kiddy1/><Kiddy2/></Root>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-21 12:23 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Better memory management was more important than this. The objects are limited to scope and since you cannot cast objects, you would need to persist the newly created objects objects in some way. i.e.:

<?php

class TplCompileTree extends DomDocument {}
class TplTagDefault extends DomElement {}

$docRoot = &new TplCompileTree();
$newNode = &new TplTagDefault('Root');
$docRoot->appendChild($newNode);

$doc_array = array();

for($i = 0; $i < 3; $i++) {
    $doc_array[] = $newNode->appendChild(new TplTagDefault('Kiddy'.$i));
}
foreach($docRoot->childNodes AS $child) {
    var_dump($child);
    foreach($child->childNodes as $kids) {
        var_dump($kids);
    }
}
echo $docRoot->saveXML();

?> 
 [2004-10-22 12:56 UTC] Jason at amp-design dot net
There is no mentioning of this is the documentation for PHP, so although this may not be a bug in PHP it's self, it's a bugin the documentation if it doesn't mention this. Should I submit this as a documentation bug under a new ID?

Regards
Jason
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC