Jump to content

[SOLVED] Member function on a non-object within a function! ($this-> syntax)


Dale_G

Recommended Posts

Hello everyone, right so I am including an external file as such...

 

require( 'members.php' );

 

And I've confirmed it works, using this syntax everything is fine.

 

echo $members->show('all');

 

It shows all of the members, it does what it is supposed to do. However, when I throw it into a function and attempt to call the function, as shown below...

 

function show_members()
{
    echo $members->show('all');
}

show_members();

 

It does not work and throws a "Fatal error: Call to a member function on a non-object" error, and I know why. I can remedy this problem by adding "global $members;" to the function, like this.

 

function show_members()
{
    global $members;

    echo $members->show('all');
}

 

So now, the completed working file is shown below.

 

require( 'members.php' );

function show_members()
{
    global $members;

    echo $members->show('all');
}

 

show_members();

 

And that's fine. However as I develop this project there will be MANY choices that require different calls to different functions, so I will be making over 30 functions similar to show_members(), that call different functions under the $members class.

 

Basically, what I'm wondering is if there is a way to "globally" declare the $members class. So I don't have to type "global $members;" in each function...as that can get rather bothersome the more functions I add.

 

Also, I will probably throw this into a class later on, so if there is a way to globally declare it for the class, that would be awesome. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.