<!DOCTYPE html> <html> <head> <title></title> </head> <body> <?php class Mysql_1{ private $localhost,$name,$password,$database,$time_1; public function __construct(){ $this->localhost="localhost"; $this->name="root"; $this->password="root"; $this->database="test"; $this->time_1=date("Y-m-d H:i:s"); } public function connect(){ @mysql_connect($this->localhost,$this->name,$this->password,$this->database) or die("数据库连接失败!"); @mysql_select_db($this->database) or die("选择数据库失败!"); mysql_query("set names utf8"); } public function getTime_1(){ return $this->time_1; } } class Mysql_2 extends Mysql_1{ private $time_2; public function setTime_2(){ $this->time_2=date("Y-m-d H:i:s"); } public function getTime_2(){ return $this->time_2; } public function getTime_1(){ return parent::getTime_1(); } } $mysql_1=new Mysql_1(); echo "time_1: ".$mysql_1->getTime_1()."<br>"; $mysql_2=new Mysql_2(); echo "setTime_2! ".$mysql_2->setTime_2()."<br>"; echo "time_2: ".$mysql_2->getTime_2()."<br>"; echo "time_1: ".$mysql_2->getTime_1()."<br>"; ?> </body> </html>
本文主要讲类的继承当中的extends 和 parent。