Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. Multiple inheritance in PHP PHP does not support Multiple Inheritance but using interfaces in PHP, we can implement it. Using class with Traits: The trait is a type of class that enables multiple inheritance. Classes, objects, and traits that do not extend more than one class, but it can extend multiple traits at the same time. Syntax:
class child_class_name extends parent_class_name
{
    use trait_name;
   ------
   ------
    child_class functions
}
Example:
<?php
// Class Inheritance
class Inheritance
{
public function example()
{
            echo "Hi";
}
}
// Trait formultiple
trait formultiple
{
public function examplem()
{
            echo "\t Traits";
}
}
class Sample extends Inheritance
{
use formultiple;
public function MultipleInheritance()
{
            echo "\n Multiple-Inheritance";
}
}
$check = new Sample();
$check->example();
$check->examplem();
$check->MultipleInheritance();
?>
Output:
Hi Traits Multiple-Inheritance
  • In the above example, “traits” is used with parent class. There is “class” named “Inheritance” which contains function example() and “trait” named “formultiple” that contains function “examplem()” and a child class named “Sample” and we are creating the object of this class named “check” and we are invoking all the functions of a class and a trait.

Using Multiple Traits:

Multiple Traits can be added into a class by listing them in the use statement, which is separated by commas. Syntax:
class child_class_name 
{
use trait_name;  
----    
----    
child_class functions
}
Example:
<?php
// trait Multiple
trait Multiple
{
public function example()
{
            echo "Hii";
}
}  
trait formultiple
 {
public function exam()
{
            echo " Multiple";
}
}
class Test
{
use Multiple;
use formultiple;
public function MultipleInheritance()
{
            echo "\n MultipleInheritance";
}
}
$check = new Test();
$check->example();
$check->exam();
$check->MultipleInheritance();
?>
Output:
Hii Multiple MultipleInheritance
  • In the above example, the “traits” have been used. There are two traits named “Multiple” that contain function example() and “multiple” that include function exam(), and there is a child class “Test” and we are creating the object of that class named “check” and we are invoking all the functions of traits.

Using Class along with Interface

Syntax:
class childclass extends parentclass implements interface1, …
Example:
<?php
class A
{
public function insideA()
{
            echo "A";
            }
}
interface B
 {
public function insideB();
}
class Multiple extends A implements B
 function insideB()
{
                        echo "\n Interface";
            }
public function insidemultiple()
{
            echo "\n Inherited";
            }
}
$check = new multiple();
$check->insideA();
$check->insideB();
$check->insidemultiple();
?>
Output:
A Interface Inherited
  • In the above example, Interface “B” used with the class “A” to implement multiple inheritances. The important point is that it cannot define the function inside the interface; it should be set inside the child class “Multiple”. We are calling all the functions using the child (Multiple) objects named “check”.

Multiple Interface

Example:
<?php
interface C
{
public function interfaceC();
}
interface B
 {
public function interfaceB();
}
class Multiple implements B, C
{
// Interface B
            function interfaceB()
 {
                        echo "\n InterfaceB";
            }

// Interface C
            function interfaceC()
 {
                        echo "\n InterfaceC";
            }

            public function insidemultiple()
            {
                        echo "\n Inherited class";
            }
}
$test = new multiple();
$test->interfaceC();
$test->interfaceB();
$test->insidemultiple();
?>
Output:
InterfaceC InterfaceB Inherited class
In the above example, multiple interfaces have been used to implement multiple inheritances. There are two interfaces called “B” and “C” that are base classes with the child class that is called “Multiple” and we are calling the functions using its object "test".

Related Topics

PHP strstr() Function

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

2 minutes read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP is_numeric() Function

The is_numeric() function in PHP finds whether the specified variable is a number or a numeric string. It returns a Boolean value TRUE if the specified var is a number or a numeric string else it returns FALSE otherwise. Syntax is_numeric ( mixed...

1 minute read.

PHP is_bool() Function

The is_bool() Function in PHP is used to  find whether a variable is a Boolean or not. It returns a Boolean value true if the parameter var is a Boolean else it returns...

1 minute read.

PHP chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.