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 in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP rtrim() Function

PHP rtrim() Function The rtrim() function in PHP is used to strip off the whitespace (or other characters) from the end of a string. If the second parameter is not specified, this function will strip are...

1 minute read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP array_key_exists() function

The array_key_exists () function in PHP is used to check whether the specified key exists in the array or not. It returns a Boolean value TRUE if the given key exists else it returns FALSE. Syntax array_key_exists(mixed key,array$array) Parameter key(required)- This...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

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.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP shuffle() Function

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays 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 soundex() Function

PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...

1 minute read.

PHP natsort() Function

PHP natsort() Function The natsort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natsort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean value TRUE on success or FALSE on...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.