Req #27418 [Opn->Fbk]: SimpleXML Object forgetting values.

From: Date: Thu, 08 Jan 2015 22:49:24 +0000
Subject: Req #27418 [Opn->Fbk]: SimpleXML Object forgetting values.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189765@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=27418&edit=1

 ID:                 27418
 Updated by:         ajf@php.net
 Reported by:        wb at pro-net dot co dot uk
 Summary:            SimpleXML Object forgetting values.
-Status:             Open
+Status:             Feedback
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 Operating System:   *
 PHP Version:        5.0.0b4 (beta4)
 Block user comment: N
 Private report:     N

 New Comment:

Does SimpleXML's ->__toString() work for you?


Previous Comments:
------------------------------------------------------------------------
[2004-03-02 11:02:13] 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.

------------------------------------------------------------------------
[2004-03-02 10:17:02] 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 08:10:51] 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-02-27 09:43:44] 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
(
) 


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=27418&edit=1


Thread (6 messages)

« previous php.bugs (#189765) next »