php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49251 wrong object created
Submitted: 2009-08-14 13:49 UTC Modified: 2009-09-25 18:32 UTC
From: ies_clan at hotmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.*, 6 (2009-08-17) OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ies_clan at hotmail dot com
New email:
PHP Version: OS:

 

 [2009-08-14 13:49 UTC] ies_clan at hotmail dot com
Description:
------------
if u try to create a new object from an simple xml-objekt-property you got an simple xml object :(

Reproduce code:
---------------
class test {}

$Xml = new SimpleXMLElement('<root><classname>stdclass</classname></root>');
$XmlClass = new $Xml->classname('<root />');
print_r($XmlClass);

$MyClass = new stdclass();
$MyClass->classname = 'test';
$stdclass = new $MyClass->classname();
print_r($stdclass);


Expected result:
----------------
stdclass Object
(
)
test Object
(
)


Actual result:
--------------
SimpleXMLElement Object
(
)
test Object
(
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-17 18:19 UTC] jani@php.net
Quite weird indeed. And verified in all branches.
 [2009-08-17 18:22 UTC] jani@php.net
And it only happens with 'new'.
 [2009-09-25 18:32 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

simpleXML returns simpleXML objects when accessing properties of the 
document, and calling new on the returned object, creates a new object 
of that type returned.

You need to cast to string before calling new, i.e.:
$mclass = (string)$Xml->classname;
$XmlClass = new $mclass('<root />');
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 19:01:30 2024 UTC