Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got error of "Call a member function embed() on a non-object", but I can see the result of var_dump is an object. Please help evaluate the following code:

This is the Class to create a picture:

Class Picture{
//....
function embed() {...}
//....
}

This is the Class for template:

Class Picture_Template{
$obj_id = $post->ID;
$pic = New Picture( $obj_id );
$this->obj = $pic;
}

This is a function to test the code:

function test_picture(){
global $pic_template;
$pic_template = new Picture_Template();
$obj =$pic_template->obj;
$obj_func =$obj->embed();
echo '<pre>';
var_dump( $obj ); // I can see it is an object
var_dump( $obj_func ); // I can see the embed() function output correct result
}

This is the function that get the error message:

function play_button(){
echo get_play_button();
}

function get_play_button(){
global $pic_template;
if (!$obj )
 $obj =& $pic_template->obj;
if ( $obj->embed(){
//do something
}
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

To me it seems Picture_Template is worthless - it's not even correctly formed (you need to place run-time code in a constructor).

Plus you also seem to lack the basic understanding of variable scope (like accessing $post and $obj within functions and classes, without globalising them first).

Here is what I think you're trying to do;

function test_picture()
{    
    $picture = new Picture( get_the_ID() );
    $picture_result = $picture->embed();

    /* debugging */
    echo '<pre>';
    var_dump( $picture );
    var_dump( $picture_result );
    echo '</pre>';
}

function play_button()
{
    echo get_play_button();
}

function get_play_button()
{
    $picture = new Picture( get_the_ID() );

    if ( $embed = $picture->embed() ) {
        // do something & return something
    }

    return null; // return nothing if embed failed
}
share|improve this answer
    
there are lots of variables in the picture object, that's why I use a Picture Template Class-- can use $template->color, $template->width..., Otherwise, I will need $picture= new Picture(get_the_ID) in each function to get each variable. But I will try your method first, to see how it works. Thanks! –  Jenny Jun 19 '11 at 11:10

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.