William Jiang

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

PHP class: inheritance or import?

When applying PHP 3rd-party tools into application, such as Smarty template, we have at least 2 choice:

  • Extend our class as a sub-class of Smarty
    This is like 1 big object to hold different resources, orgnized by associated tables (hash tables) inside this object.
  • Import Smarty into our codes, create a Smarty object aside within our class.
    This is like 2 seperated objects to hold each properties and methods: 1 is Smarty object, 1 is our own class object.

From the point of security, re-use and non memory-conflict, I prefer to the first. By using cascade classes, it is easy to extend to implment different business logic.
(1) The basic class is like:

class SmartyExtendBase extends Smarty {
 var $mdb2, $template_dir, $compile_dir, $config_dir, $cache_dir;	
 function __construct() {
  $this->mdb2 = $this->pear_connect_test();
  $this->caching = false;
  $this->template_dir = TEMPLATES;
  $this->compile_dir = TEMPLATES_C;
  $this->config_dir = CONFIGS;
  $this->cache_dir = CACHE;

(2) The extend class is:

class ExtendClass extends SmartyExtendBase, FPDF
 var $url, $html, $div, $data, $ini_array, $fpdf;
 public function __construct() {
  $this->url = $_SERVER['PHP_SELF'];
  $this->html = substr(strrchr($this->url, '/'), 1, -4);
  $this->div = PAGINATION;
  $this->data = array();
  $this->sql = "SELECT count(*) FROM " . TEST;
  $this->ini_array = array();

In the child class, it inherits Smarty object, FPDF object, as well as it’s own property and methods, a Big powerful object to use.


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: