LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP workaround for multiple inheritance? (https://www.linuxquestions.org/questions/programming-9/php-workaround-for-multiple-inheritance-702751/)

konqi 02-06-2009 10:23 AM

PHP workaround for multiple inheritance?
 
I know PHP doesn't support multiple inheritance. But I need a way to use functions between two or more classes:

Code:

class one
{
  function test1()
  {
    echo "one";
  }
}

$one = new one;

class two
{
  function test2()
  {
    $one->test1(); //This doesn't work

    global $one;
    $one->test1(); //Offcourse this does work

    one::test1(); //This also works

    // Using `class two extends one` also works, but what when i
    // wan't to use functions from a third class? It can't be done.
  }
}

I don't want to use 'global' in each function to make the class global. I prefer not to use one::test1(), because i wan't to keep working with objects. Although when there aren't any solutions, i can use it. Like i said before using `extends` isn't an option.

I also heard some people about using interfaces. But when you use interfaces, you still need to define the functions in each class.

So how should i do a workaround for multiple inheritance in PHP?

tardigrade 03-07-2009 09:42 PM

No direct answer
 
It really depends on what exactly you are doing as to what will make sense. Can you make any of the functions static?
http://us.php.net/manual/en/language.oop5.static.php
Can something be made into a singleton? Check out some design patterns with php.
http://www.ibm.com/developerworks/li...p-designptrns/

sundialsvcs 03-07-2009 10:15 PM

And to a certain extent, "a language is what a language is..."

graemef 03-08-2009 04:10 AM

You can tackle this through association. Have an instance of class one in class two and then you will be able to do $this->one->test1();


All times are GMT -5. The time now is 12:36 AM.