Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Any idea why this would happen?

code:

<?php
print_r($this->$property);
var_dump($this->$property[0]);
?>

output:

Array
(
    [0] => WP_Post Object
        (
            [ID] => 34901
            [post_author] => 1
            [post_date] => 2013-01-04 21:04:34
            [post_date_gmt] => 2013-01-05 02:04:34
            [post_content] => 
            [post_title] => Castro Theater  
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => castro-theater
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2013-01-04 21:04:34
            [post_modified_gmt] => 2013-01-05 02:04:34
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://demo.gala.local/2012/venues/castro-theater/
            [menu_order] => 0
            [post_type] => venue
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [p2p_id] => 34444
            [p2p_from] => 34891
            [p2p_to] => 34901
            [p2p_type] => scheduleitem_to_venue
        )

)
NULL
share|improve this question
    
If you are accessing a class property, you shoudl $this -> property[0] –  Starx Jan 13 '13 at 3:10
    
Please do not use foul words to avoid being flagged. –  Starx Jan 13 '13 at 3:10
    
@Starx $property is a variable that contains the name of the property, not the property itself. –  jessica Jan 13 '13 at 3:10
    
Can you access the property directly without the variable? –  Starx Jan 13 '13 at 3:11
    
@Starx If you will notice from my example, I can access it using the variable, just not one of the elements inside. –  jessica Jan 13 '13 at 3:15

1 Answer 1

up vote 1 down vote accepted

Your code ends up processesing $property[0] first, then tries to fetch the property of your object with the result of that. If you get $this->$property first, then you can fetch things using the normal array notation and things will work as you expect.

$data = $this->$property;
var_dump($data[0]);
share|improve this answer
    
Ah, I get it. Thanks! I have fixed it by using $this->{$property}[0] –  jessica Jan 13 '13 at 3:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.