Search code examples
phpobjectvar-dump

Have not access to the element of object without var_dump at front


I'm having a very strange problem. The thing is, that I can't access anything from the variable $conDat["_RenewalDate1"] unless I use an var_dump of it upfront.

With this code:

var_dump($conDat["_RenewalDate1"]);

$test = $conDat["_RenewalDate1"] -> date;
var_dump($test);

I get the desired output.

But without this line:

var_dump($conDat["_RenewalDate1"]);

It just returns NULL.

What could possibly be the cause of this strange problem?

Additional info:

This code:

echo "first var_dump: <br />";
var_dump($conDat["_RenewalDate1"]);

$renewalDate1 = $conDat["_RenewalDate1"] -> date;
echo "<br />second var_dump: <br />";
var_dump($renewalDate1);

output:

first var_dump: 
object(DateTime)#47 (3) { 
    ["date"] => string(26) "2015-10-11 00:00:00.000000" 
    ["timezone_type"] => int(3) 
    ["timezone"] => string(3) "UTC" 
} 
second var_dump: 
string(26) "2015-10-11 00:00:00.000000"

and this:

echo "first var_dump: <br />";
// var_dump($conDat["_RenewalDate1"]);

$renewalDate1 = $conDat["_RenewalDate1"] -> date;
echo "<br />second var_dump: <br />";
var_dump($renewalDate1);

output:

first var_dump: 

second var_dump: 
NULL

Solution

  • $conDat["_RenewalDate1"] is a DateTime object. You're attempting to use an undocumented property that is only ever made available due to a side effect.

    ->date being available is actually a side-effect of support for var_dump()


    Use format() instead.