keybase
[m6w6/m6w6.github.io] / _posts / 2004-08-03-class-inheritance-or-containers.md
1 ---
2 title: Class inheritance or Containers?
3 author: m6w6
4 tags:
5 - PHP
6 ---
7
8 While talking with Daniel and Lorenzo about "Tree Reincarnated" I regularly
9 stumble across the question what approach to use to implement different
10 backends: **"Class inheritance or Containers?"**.
11
12 I personally feel that using the container approach leads to massive code
13 duplication, while using class inheritance avoids the most (_most_, because
14 there's no real multiple class inheritance in PHP).
15
16 ## Container
17 ```php
18 class PublicAPI
19 {
20 var $container; // SpecificAPI
21
22 function doSomething()
23 {
24 return $this->container->doSomething();
25 }
26 }
27 ```
28
29 ## Class Inheritance
30
31 ```php
32 class SpecificAPI extends PublicAPI
33 {
34 function doSomething()
35 {
36 // do something only this class needs to do
37 }
38 }
39 ?>
40 ```
41
42 So what do you feel about this topic and how you'd handle that?
43
44 ## UPDATE
45
46 While looking again at Daniel's [nice diagram](http://devel.webcluster.at/~daniel/pear/Tree/docs/Tree.png?),
47 I feel like we've already reached the point where solving this problem with class
48 inheritence is impossible (despite using aggregate*() - you're right Lorenzo,
49 again :) have nice holidays! ).
50
51 Tree_Admin_Simple_MDB2 would have to extend Tree_Simple_MDB2 **and**
52 Tree_Admin_Simple_RDBMS...
53
54 Mike's arrived at the dead end, again ;)