78931

How can I extend 2 classes in PHP? [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/90982/multiple-inheritance-in-php" rel="nofollow">Multiple Inheritance in PHP</a><br /><a href="https://stackoverflow.com/questions/356128/can-i-extend-a-class-using-more-than-1-class-in-php" rel="nofollow">Can I extend a class using more than 1 class in PHP?</a>

</blockquote>

Does anyone know how can I extend 2 classes from one class?

Example: How can I add another class called classB into the following class?

<?php class Test extends classA { echo "test"; } ?>

I tried the following code, but this is not work:

<?php class Test extends classA, classB { echo "test"; } ?>

Anyone know how can I solve this issue?

Answer1:

PHP, like Java, does not support multiple inheritance.

Coming in PHP 5.4 will be <a href="https://secure.php.net/manual/en/language.oop5.traits.php" rel="nofollow">traits</a> which attempt to provide a solution to this problem.

In the meantime, you would be best to re-think your class design. You can implement multiple interfaces if you're after an extended API to your classes.

Answer2:

PHP doesn't really support multiple inheritance, but there are some (somewhat messy) ways to implement it. Check out this URL for some examples:

<a href="http://www.jasny.net/articles/how-i-php-multiple-inheritance/" rel="nofollow">http://www.jasny.net/articles/how-i-php-multiple-inheritance/</a>

Answer3:

What you're asking about is called "Multiple Inheritance". PHP doesn't support this, however you'd do well to have a read through this StackOverflow thread:

<a href="https://stackoverflow.com/questions/90982/multiple-inheritance-in-php" rel="nofollow">Multiple Inheritance in PHP</a>

Recommend

  • Inheritance in std::map with base class as value
  • objective C: use NSMutableArray in different classes
  • Jaxb - Overriding the XMLElement name attribute
  • C++ Pointer to Member Function as Template Default Argument
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Problems compiling files using JOGL
  • HTTP/2 streams vs HTTP/1.1 connections
  • Hardware Accelerated Image Scaling in windows using C++
  • Python urlparse: small issue
  • Extracting HTML between tags
  • Highlight and Bold text in JTextPane
  • Repeat a vertical line on every page in Report Builder / SSRS
  • how to do an event when i swipe from fragment to the other
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Bug in WPF DataGrid
  • Cross-Platform Protobuf Serialization
  • Can I make an Android app that runs a web view in Chrome 39?
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Jquery - Jquery Wysiwyg return html as a string
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to format a variable of double type
  • How to include full .NET prerequisite for Wix Burn installer
  • Acquiring multiple attributes from .xml file in c#
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • JaxB to read class hierarchy
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal