Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •