William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

PHP: class inheritance

For a large project, to compact, clear codes are very helpful for modifying, management and maintenance. The following is a snippet to make codes short, clear in a large project.

in a Parent-Children Class environment, instead of initializing varaibles in each sub-class, we refine the common varaibles in a higher class’ construct function, then using parent::_construct() to extend and instance when creating class. The following example illustrates the implementation:
1. in parent-class ‘BaseClass.inc.php’:

class BaseClass extends Smarty
 var $url, $self, $session, ...;
 function __construct() {
  // do Smarty construct.
  $this->url = $_SERVER["PHP_SELF"];
  // will extend in each sub-class.
  $this->self = basename($this->url, '.php');
  // will be overwritten in each sub-classes when create instances.
  $this->session = array(
   'sql' => $this->self.'_sql',
   'rows' => $this->self.'_rows',
   'magic_sql' => $this->self.'_magic_sql',

2. in sub-class ‘ListBase.php’, in its construct() function, declares the class variables, then initialize:

class ListBase extends BaseClass
 // decalre class variables.
 var $url, $self, $session,;
 // when initializing, executes parent construct.
 function __construct() {

This way, when this class initializes, parent::__construct() will guarantee all the variables: $url, $self, $session will point to ‘ListBase.php’, not parent-class ‘BaseClass.php’.

3. in sub-sub-class ‘UsersClass.php’, the same way: declares the class variables, then initialize.

class UsersClass extends ListAdvanced
 var $url, $self, $session;
 public function __construct() {

Remember, the declares is important, it indicates these variables are belong to this class, not parent class or anyplace else. Can’t be ignored.
This maybe the most compact way to extend sub-class with same varaibles(class properties) but different values. Very useful for multi-inheritances and lots of sub-classes with same parent class. For examples, a class has more than 20 sub-classes, using this will save codes.


2 responses to “PHP: class inheritance

  1. Mohit 09/28/2011 at 8:47 pm

    Nice information. Thanks . I thought that the base class constructor is automatically called.

    • williamjxj 09/28/2011 at 9:39 pm

      Actually not auto calling, it has to be manually called. The following is a simple example to see the necessary of parent::__construct();
      comment parent::__construct() in parent class, and you will find some variables missing.

      class Base {
      var $var1, $var2;
      function __construct() {
      $this->var1 = ‘abc’;
      $this->var2 = ‘edf’;

      class ListClass extends Base {
      var $var1, $var2, $var3, $var4;
      function __construct() {
      $this->var3 = ‘3333’;
      $this->var4 = ‘var4’;
      class User extends ListClass {
      var $var1, $var2, $var3, $var4, $var5, $var6;
      function __construct() {
      $this->var5 = ‘5555’;
      $this->var6 = ‘6666’;
      $u = new User();
      echo “<pre>”; print_r($u); echo “</pre>”;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: