Server-Side Magazine

How to Inherit from Multiple Objects (Workaround)

As you may know, inheriting from multiple objects in PHP 5 is impossible, because of the language restrictions. You can’t write code such as this:

1
2
3
class Child extends Mother, Father {
	//class code here
}

With a little workaround, eventually we could mimic multi-object inheritance, although it won’t be true multiple inheritance, but it will be pretty close. First let me show you the classic approach of fixing this problem then the new method of inheriting from many objects.

Classic Approach

By stacking up classes and inheriting from each other, it is possible to achieve some sort of inheritance, but this means you sacrifice the manageability and reusability principles of object oriented programming.

The image below illustrates the classic approach.

Object Inheritance Classic Approach

This could be coded in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class A {
	public function class_a() {
		echo 'this is class a';
	}
}
 
class B extends A {
	public function class_b() {
		echo 'this is class b';
	}
}
 
class C extends B {
	public function class_c() {
		echo 'this is class c';
	}
}
 
class D extends C {
	public function class_d() {
		echo 'this is class d';
	}
}
 
$obj = new D();
 
$obj->class_a();
$obj->class_b();
$obj->class_c();
$obj->class_d();

By implementing this kind of architecture the web project would be doomed from start. Instead let’s have a look at the other approach.

New Method

The idea behind this method is to create an abstract super object which makes it possible to plug-in objects and by instantiating it, all the plugged-in objects’ properties and methods would be available. This is possible with the help of dynamic variable creation.

The classes would be inherited such as the image illustrates below:

Object Inheritance new Method

Dynamic variable creation is a really cool feature in PHP, just as we need to create class properties of classes in our SuperObject class. Let’s build this object creation model. First we create a couple of sample classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class A {
 
   public function class_a() 
   {
      return 'this is object a';
   }
}
 
class B {
 
   public function class_b() 
   {
      return 'this is object b';
   }
}
 
class C {
 
   public function class_c() 
   {
      return 'this is object c';
   }
}

Then we create the SuperObject class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
abstract class SuperObject 
{
   public function __construct() 
   {
      if (func_num_args() > 0)
      {
         $arr = func_get_args();
 
         for ( $i=0,$m=count($arr); $i<$m; $i++ )
         {
            $class_name = $arr[$i];
 
            $this->extendClass($class_name);
         }			
      }
   }
 
   public function extendClass($class_name)
   {
      $this->$class_name = new $class_name;
   }	
}

The class constructor checks if we passed any class names, if yes then it loops through this array and extends it’s object with these classes. Every class loaded will have a designated dynamic variable in the SuperObject class.

In the extendClass function is where the magic happens. You see the line:

20
$this->$class_name = new $class_name;

Usually we refer to class properties like this:

1
$this->class_prop

But note the extra $ (dollar) sign before the class_name in the extendClass function. This is how dynamic variables are created. The actual dynamic variable name will the value of the passed variable.. For example:

1
2
3
$test = 'hello';
 
$$test = 'world';

Here, $$test name will be the value of the $test, so $hello will be created and it will have the value of $$test which is world. It’s a little hard to understand at first, but once you get the basic idea you’ll find that it’s easy to play with dynamic variables. This same principle applies to our SuperObject. If we load the extendClass function like this:

1
$this->extendClass('A');

Then this function would create a property in the SuperObject like $this->A and it will instantiate the class called A.

As a last step, we need to inherit from the SuperObject class. Let’s create a new class and extend it:

1
2
3
4
5
6
7
class MyObject extends SuperObject
{
   public function __construct()
   {
      parent::__construct('A','B');
   }
}

When we instantiate the MyObject class it will load the A and B classes. Optionally we could load the class C to and plug it in to our object structure.

1
2
$mo = new MyObject();
$mo->extendClass('C');

To access the loaded objects properties and methods, we simply call it from our MyObject instance like this:

1
2
echo $mo->A->class_a();
echo $mo->B->class_b();

Related Posts

Tags: , , , ,

About the Author

Server-Side Magazine

Server-Side Magazine is a place where users can contribute articles. Strictly server-side posts are presented in the following programming languages: PHP, Ruby, ASP.Net, Java, Python

Our philosophy is that knowledge should be free and available to anyone. We designed this site to be an open platform, meaning that anyone can contribute and share their knowledge on the above areas.

Although, it's an open platform we don't accept all articles, because it would result in a "just another", mediocre website. A minimum standard of quality is required from every submitted article.

Comments

Leave a Comment

Your email address will not be published. Required fields are marked *

Markdown enabled. Click here to see the syntax.

 
More in PHP (3 of 9 articles)