<?php abstract class ParamHandler{ protected $source; protected $param = array(); function __construct($source) { $this->source = $source; } function addParam($key,$val) { $this->params['$key'] = $val; } function getAllParams() { return $this->params; } static function getInstance($filename){ //用來獲取子類實例,文件類型多時,只 //需在這裏加上if語句,而後對應寫上子類 if (preg_match("/\.xml$/i", $filename)) { return new XmlParamHandle($filename); } return new TextParamHandler($filename); } abstract function write(); //保證子類實現這兩個方法 abstract function read(); } ?>
而後下面是子類php
class XmlParamHandle extends ParamHdndle{ function write(){} function read(){} } class TextParamHandle extends ParamHdndle{ function write(){} function read(){} }
這樣實例化的時候能夠這樣子ide
$test = ParamHandler::getInstance("./params.xml");this
這樣根據傳入的params.xml文件會找到對應的類實例化。xml