How to call functions from another class.

Many of my articles and videos are based on questions that I see popping up in the php forums or in emails sent to me personally.

Recently I was asked by someone how they could call a function found in one class, in another. This may seem like basic stuff to those of us who know … but please keep in mind, at one time, none of us knew anything!


Anyway, here is my video on using a function from another class in a class.

This entry was posted on Saturday, July 19th, 2008 at 10:26 pm and is filed under Beginners PHP Articles, Object Oriented PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

5 Responses to “How to call functions from another class.”

  1. Eric says:

    Thank you for this tutorial. My question is, how do you call a function from another class if the classes are both instantiated from outside the classes (out at index.php, for example)?

    For example, I have my class “website”, which executes session_start() among other things. My website class also houses encryption methods.

    In my “pages” class, which I instantiate to create a page, it needs access to these encryption methods.

    So these are instantiated from index.php, and I can’t instantiate website class twice, since it runs code that should only be executed once per page load.

    I could pass my “website” object path when instantiating “pages”, but the code:

    class page
    public $website
    function __construct($website)

    Fails to work. How would one get around this?

  2. Eric says:

    You know what? I don’t need to use $this when referencing variables passed into the methods in classes. I think by removing “this->”, the code example will work. I answered my own question πŸ˜€

    Thanks again!

  3. Stefan Mischook says:

    Glad I could help!!



  4. Rob says:

    I have a possibly similar question about already instantiated classes. Based on what I have seen on other sites I have something like this:

    class login
    var $loginID; var $role; ….
    function getCurrentLogin() {$this->loginID=…. }

    function displayUserOptions() {echo “Welcome “.this->name …}

    class navigation
    var currentPage; var menuStructure; ….

    function displayNavigation($currentLogin)
    code to display menus;

    $_SESSION[“$currentLogin”] = new login;}

    $_SESSION[“$navigation”] = new navigation;}

    The navigation bar displays fine then I get Call to undefined method login::displayUserOptions()

  5. Stefan Mischook says:


    Can you repost your question on the PHP forum:



Created By Stefan Mischook  |  Top of page  go to top of page