LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2009, 10:23 AM   #1
konqi
Member
 
Registered: Oct 2007
Distribution: Gentoo, Slackware
Posts: 162

Rep: Reputation: 16
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?
 
Old 03-07-2009, 09:42 PM   #2
tardigrade
Member
 
Registered: Jun 2004
Distribution: SuSE 9.1
Posts: 150

Rep: Reputation: 15
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/
 
Old 03-07-2009, 10:15 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,609
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
And to a certain extent, "a language is what a language is..."
 
Old 03-08-2009, 04:10 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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();
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Inheritance with Multiple Files, #include crap Hard Help please byteframe Programming 10 12-08-2005 07:04 PM
PHP: Browser timeout workaround?? jpbarto Programming 2 08-05-2005 10:30 AM
php registerglobal = off workaround? stelar Linux - Security 2 06-30-2003 02:04 AM
Multiple Inheritance in Java mikeshn Programming 2 02-25-2002 08:13 PM
RAD tools revisited: Multiple Inheritance and the Web Citizen Bleys Programming 1 02-11-2002 01:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration