0

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
}
}

1 Answer 1

1

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
}
1
  • 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, 2011 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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