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.

Tell a friend about this article / post.

3 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)
    {
    $this->website->encrypt();
    }
    }

    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 :D

    Thanks again!

  3. Stefan Mischook Says:

    Glad I could help!!

    ;)

    Stefan

Leave a Reply