1

stdClass 値を変数に割り当てる方法を知っている人はいますか?

stdClass オブジェクトがあり、 を使用var_dump($userdetails->emailaddress)して出力すると、値が String(31)"asdas.@fsdf.com"; として出力されます。

しかし、オブジェクトの値を変数に割り当てようとすると、次のようになります。

$to = $userdetails->emailaddress; 

$to値がNULLになる...

誰でも助けることができますか?

4

3 に答える 3

5

That sounds like you are doing something wrong, because

$obj = new StdClass;
$obj->email = 'foo@example.com';
$to = $obj->email;
var_dump($to); // string(15) "foo@example.com"

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

于 2010-08-09T10:40:21.503 に答える
0

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

于 2010-08-09T10:39:56.870 に答える
0

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.

于 2010-08-09T10:40:25.200 に答える