Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2011, 10:06 PM   PM User | #1
Vcize
New to the CF scene

 
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Vcize is an unknown quantity at this point
Bizarre Problem....object is empty unless I print_r or vardump first

I have an object that looks like this:

Code:
[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

PHP Code:
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..

PHP Code:
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?

Last edited by Vcize; 08-22-2011 at 10:14 PM..
Vcize is offline   Reply With Quote
Old 08-23-2011, 02:38 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:33 PM.


Advertisement
Log in to turn off these ads.