How to call functions from another class.

July 19, 2008

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.

5 Responses

  1. Eric Author January 22, 2009 at 6:47 pm Reply

    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 Author January 22, 2009 at 6:53 pm Reply

    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 Author January 22, 2009 at 7:14 pm Reply

    Glad I could help!!

    😉

    Stefan

  4. Rob Author July 16, 2009 at 8:39 am Reply

    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.php
    class login
    {
    var $loginID; var $role; ….
    function getCurrentLogin() {$this->loginID=…. }

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

    —class.navigation.php
    class navigation
    {
    var currentPage; var menuStructure; ….

    function displayNavigation($currentLogin)
    {
    code to display menus;
    $currentLogin->displayUserOptions();
    }

    —-index.php
    include(class.login.php);
    include(class.navigation.php);
    if(!isset($_SESSION[“$currentLogin”])){
    $_SESSION[“$currentLogin”] = new login;}
    $_SESSION[“$currentLogin”]->checkOrGetCurrentLogin();

    if(!isset($_SESSION[“$navigation”])){
    $_SESSION[“$navigation”] = new navigation;}
    $_SESSION[“$navigation”]->displayNavigation($_SESSION[“$currentLogin”])

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

  5. Stefan Mischook Author July 16, 2009 at 9:32 pm Reply

    Hi,

    Can you repost your question on the PHP forum:

    http://www.killersites.com/forums/forum/12/php/

    Thanks,

    Stefan

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top