...

View Full Version : Bizarre Problem....object is empty unless I print_r or vardump first



Vcize
08-22-2011, 11:06 PM
I have an object that looks like this:


[createdDatetime:Legacy\CdyneAddressReturns:private] => DateTime Object
(
[date] => -0001-11-30 00:00:00
[timezone_type] => 3
[timezone] => America/Chicago
)

At least, that's what it looks like it I print_r the object. However, if I don't run a print_r or a var_dump on the object first, it appears to be empty, or at least I can't access it.

For instance, when I run


foreach($currentRecord as $key => $value)
{
$dateVar = $value->getCreatedDatetime();
$createdDate = $dateVar->date;

echo "createDate is " . $createdDate . "<br/>";
}

Then $createDate is empty, and I get an undefined property error (Notice: Undefined property: DateTime::$date).

However, if I simply stick a print_r at the top..


foreach($currentRecord as $key => $value)
{
$value->getCreatedDatetime();

$dateVar = $value->getCreatedDatetime();
$createdDate = $dateVar->date;

echo "createDate is " . $createdDate . "<br/>";
}

Then everything appears to work fine, and I get...

createDate is -0001-11-30 00:00:00

I've never run into a problem like this before where my program behaved differently based simply on whether or not I printed something out in the middle of it. Any idea what's going on here?

Fou-Lu
08-23-2011, 03:38 AM
You should never receive an Undefined Property when accessing DateTime::$date. That would be a fatal error since its not within a valid scope. The only thing I can think of that would allow this is if you are defining an alternate DateTime class for it to work with within the scope of your current namespace.
Since I cannot replicate this behaviour, and receive the correct fatal notification of attempting to access a private member, can you post the entire code in use so this can be tested as well as the version of PHP you are using.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum