php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27418 SimpleXML Object forgetting values.
Submitted: 2004-02-27 09:43 UTC Modified: 2015-01-18 04:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: wb at pro-net dot co dot uk Assigned:
Status: No Feedback Package: SimpleXML related
PHP Version: 5.0.0b4 (beta4) 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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: wb at pro-net dot co dot uk
New email:
PHP Version: OS:

 

 [2004-02-27 09:43 UTC] wb at pro-net dot co dot uk
Description:
------------
When using simpleXML you are unable to fetch some information. The information is displayed with print_r() but you can't get it it directly.

Use the example provided as an example.

Reproduce code:
---------------
<?php
header('Content-type: text/plain');

$xmlstr = '<?xml version="1.0"?>
<access>
    <user>
        <login>user1</login>
        <password>letMeIn</password>
        <site>www.pro-net.co.uk</site>
        <site>www.example.com</site>
    </user>
    <user>
        <login>user2</login>
        <password>myPassword</password>
        <site>www.pro-net.co.uk</site>
    </user>
</access>';

$nl  = "\r\n";
$xml = simplexml_load_string($xmlstr);

print('Current PHP version is : '.phpversion().$nl.$nl);

print('$xml is:'.$nl);
print_r($xml);
print($nl.$nl);

// Test authentication to get the correct user information
$login = 'user1';
$password = 'letMeIn';
print($nl.$nl.'Trying to authenticate "'.$login.'" with password "'.$password.'".'.$nl.$nl);
$isAuth = false;
foreach($xml->user as $user){
    if(utf8_decode($user->login) == $login && utf8_decode($user->password) == $password){
        $isAuth = true;
        break;
    }
}
if(!$isAuth){
    print($nl.$nl.'Invalid User.'.$nl);
    die();
}

// So lets output the variables to see what we have...
print('$user is:'.$nl);
print_r($user);
print($nl.$nl);

print('$user->site is:'.$nl);
print_r($user->site);
print($nl.$nl);

print('$user->site[0] is:'.$nl);
print_r($user->site[0]);
print($nl.$nl);

?> 

Expected result:
----------------
$xml is:
simplexml_element Object
(
    [user] => Array
        (
            [0] => simplexml_element Object
                (
                    [login] => user1
                    [password] => letMeIn
                    [site] => Array
                        (
                            [0] => www.pro-net.co.uk
                            [1] => www.example.com
                        )

                )

            [1] => simplexml_element Object
                (
                    [login] => user2
                    [password] => myPassword
                    [site] => www.pro-net.co.uk
                )

        )

)

Trying to authenticate "user1" with password "letMeIn".

$user is:
simplexml_element Object
(
    [login] => user1
    [password] => letMeIn
    [site] => Array
        (
            [0] => www.pro-net.co.uk
            [1] => www.example.com
        )

)


$user->site is:
Array
(
    [0] => www.pro-net.co.uk
    [1] => www.example.com
)


$user->site[0] is:
www.pro-net.co.uk


Actual result:
--------------
$xml is:
simplexml_element Object
(
    [user] => Array
        (
            [0] => simplexml_element Object
                (
                    [login] => user1
                    [password] => letMeIn
                    [site] => Array
                        (
                            [0] => www.pro-net.co.uk
                            [1] => www.example.com
                        )

                )

            [1] => simplexml_element Object
                (
                    [login] => user2
                    [password] => myPassword
                    [site] => www.pro-net.co.uk
                )

        )

)

Trying to authenticate "user1" with password "letMeIn".

$user is:
simplexml_element Object
(
    [login] => user1
    [password] => letMeIn
    [site] => Array
        (
            [0] => www.pro-net.co.uk
            [1] => www.example.com
        )

)


$user->site is:
Array
(
    [0] => simplexml_element Object
        (
        )

    [1] => simplexml_element Object
        (
        )

)


$user->site[0] is:
simplexml_element Object
(
) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-02 08:10 UTC] wb at pro-net dot co dot uk
I hav ebeen reading through the PHP-DEV archive and found message:

http://marc.theaimsgroup.com/?l=php-dev&m=107807524506690&w=

Which contains...

>>>> foreach($xml->user as $user){
>>>>   if (utf8_decode($user->login) == $login &&
>>>>       utf8_decode($user->password) == $password) {
>>>>       // valid users
>>>>   }
>>>> }
>>>>
>>>> Both seem like they should work, but neither do.

This infact does work in the example below with the php5 versions that i have tried. Just thought that i would point it out even if i am not quite doing things correctly (what should eb the correct way?).

My main issue/question for this bug is how do i traverse over multiple site elements for each user? And why does is go from being

[site] => Array
        (
            [0] => www.pro-net.co.uk
            [1] => www.example.com
        )

)

To:

$user->site is:
Array
(
    [0] => simplexml_element Object
        (
        )

    [1] => simplexml_element Object
        (
        )

)

depending on how you call it?
 [2004-03-02 10:17 UTC] adam at trachtenberg dot com
To parphrase Arthur C Clarke: You get different answers 
because of magic. Well, advanced PHP technology that is 
indistinguishable from magic.

The current solution to your problem is to cast the 
variable before passing it to the function, like this:

utf8_decode((string) $user->login)

It would be better if you didn't need to do this, but 
there's no clean solution to this problem right now. 
We're working on it.
 [2004-03-02 11:02 UTC] wb at pro-net dot co dot uk
Thanks for your reply. I have been playing around as well as reading some threads on the DEV mailing list.

I think the main issue is that the simpleXML documentation in the online manual is misleading and needs to be updated, this i feel would probably solve 80% of the issues :)

But the other, and to me more important, issue is the inconsistancy when using print_r() or var_dump(). They should always refer to the items as a simplexml_element Object and never as a string. The thing to remember is that the quickest way to find out what a vairable is is to just print_r() or var_dump() it. Now i feel that most people will be doing this to try to get an understanding of the new PHP5 objects, they want to see quickly what is available, and when they see a value as a string they expect to be able to use it as a string and not have it turn out to be an object later on. Alos the fact that it looks like an empty object when it is a simplexml_element dosen't help either.

I have no problem with haveing to cast the variable as a string as it does show exactly how the object is being used in that situation and rightfully "a string" != $anElement.

What about haveing the following methods to the simplexml_element Object:

    ->setText($string)
    ->getText()

Then people could use these to get and set the 'text' value for that element. I added the setValue() method because from the documentation i can't see how one would set it anyway.

Anyway those are my ideas and i would be interested to find out what people think.
 [2015-01-08 22:49 UTC] ajf@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues
 [2015-01-08 22:49 UTC] ajf@php.net
Does SimpleXML's ->__toString() work for you?
 [2015-01-08 22:49 UTC] ajf@php.net
-Package: *General Issues +Package: SimpleXML related
 [2015-01-18 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC