009-对象—— 构造方法__construct析构方法__destruct使用方法 PHP重写与重载
<?php /**构造方法__construct析构方法__destruct使用方法 PHP重写与重载 */ //构造方法:当实例化对象时,自动运行的方法 /*class channel{ function __construct() { echo 222; } } $channel=new channel();//输出:222*/ /*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); } } $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 "<br/>配置环境......<br/>"; } static function _include(){ echo "<br/>载入文件...<br/>"; } function display($content){ echo "<h1 style='color: #333; border: 3px #F00 solid;'>$content</h1>"; } public function dump($content){ echo "<pre>"; 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();