host=HOST; $this->user=USER; $this->pwd=PWD; $this->dbname=DBNAME; $this->_connect(); } function _connect(){ $this->mysqli=new mysqli($this->host,$this->user,$this->pwd,$this->dbname); }}$chanel=new db();*////*浏览器输入:http://phpbasic.com/004object/9.php?&access=admin&a=_display输出:显示栏目*//*class channel{ function __construct() { $access=$_GET['access'];//保存用户权限: if ($access=="admin"){ $method=$_GET['a']; $this->$method(); } } function _edit(){ echo "编辑栏目"; } function _del(){ echo "删除栏目"; } function _display(){ echo "显示栏目"; }}$channel=new channel();*///通过构造函数,对对象进行整体的配置:/*class APP{ function __construct() { self::_include(); self::_config(); } static function _config(){ echo "配置环境......"; } static function _include(){ echo "载入文件..."; } function display($content){ echo "$content
"; } public function dump($content){ echo ""; print_r($content); }}class channel extends APP{ function __construct() { parent::__construct(); $access=$_GET['access'];//保存用户权限: if ($access=="admin"){ $method=$_GET['a']; $this->$method(); }else{ $this->display("你没有权限,不能操作栏目"); } } function _edit(){ echo "编辑栏目"; } function _del(){ echo "删除栏目"; } function _display(){ echo "显示栏目"; }}$channel=new channel();*///析构方法:/*class db{ private $host; private $user; private $pwd; private $dbname; private $mysqli; function __construct() { include 'dbConfig.php'; $this->host=HOST; $this->user=USER; $this->pwd=PWD; $this->dbname=DBNAME; $this->_connect(); } function _connect(){ $this->mysqli=new mysqli($this->host,$this->user,$this->pwd,$this->dbname); } function update(){ echo "更新数据"; } function insert(){ echo "插入数据"; } public function _close(){ echo "关闭数据库"; } function __debugInfo() { //对象执行完之后执行的一些方法: $this->_close(); }}$chanel=new db();*///类的重写:(利用重写可以实现多态的实现)class dongwu{ function yundong() { echo "动物在跑"; }}class yu extends dongwu{ function yundong() { echo "鱼在游"; }}class niao extends dongwu{ function yundong() { echo "飞。。。。"; }}class gou extends dongwu{}class chongwu{ private $congwu; function __construct($type) { $this->congwu = new $type(); } function yundong() { $this->congwu->yundong(); }}$congwu = new chongwu('yu');$congwu->yundong();