tags:

views:

40

answers:

3

Anyone know how to assign stdClass value to variable ?

I have a stdClass object and when I print it using var_dump($userdetails->emailaddress) , it does print out the value as String(31)"[email protected]";

but when I try to assign the onject value to variable , let say : $to = $userdetails->emailaddress; the $to value become NULL ...

Anyone can help ?

A: 

I think there nothing wrong / prevent from assign value from stdClass to variable. Pls check the spelling of the code.

Bang Dao
+4  A: 

That sounds like you are doing something wrong, because

$obj = new StdClass;
$obj->email = '[email protected]';
$to = $obj->email;
var_dump($to); // string(15) "[email protected]"

Note that variables and object members in PHP are case sensitive (unlike functions and methods), so

$to = $obj->eMail;
var_dump($to); // NULL

However, in this case you also receive a PHP Notice

Notice: Undefined property: stdClass::$eMail

It is good practise to enable error_reporting(-1) and the PHP.ini directives display_errors and display_startup_errors on development machines.


Since it's a CW. feel free to use this space for further debugging tips

Gordon
A: 

Is the var public or not?

If the class is made like:

class Example
{
    public $emailaddress;

    public function example()
    {
        #do something
    }  

}

Then I can't see why there would be an issue.

Dorjan
It's a StdClass so any other visibility than public should not apply
Gordon
Ah, I have never used a StdClass so I just assumed it was an example name.My bad *blush*
Dorjan