PHP面向对象高级特性概览
本实例详细阐述了PHP面向对象程序设计中的高阶特性内容,供读者参考学习。
高阶特性概览
主要包含以下方面:
1. 静态方法与属性(无需实例化对象,直接通过类即可访问数据与功能) 2. 抽象类与接口(实现设计与实现的解耦) 3. 错误处理机制(采用异常模式) 4. Final类与方法(禁止继承或重写) 5. 拦截器(实现自动委托功能) 6. 析构方法(在对象销毁前执行清理操作) 7. 对象克隆(生成对象的副本) 8. 对象转字符串解析
提示:尝试从内存视角审视代码,构建对计算机微观世界的想象。
静态方法基础示例
<?php
class StaticExample{
static public $aNum = 10;
static public function sayHello(){
print "hello";
}
}
print StaticExample::$aNum."
";
StaticExample::sayHello();
注意事项:
1. 静态方法无法访问类中的非静态属性,因为这些属性归属于特定对象实例,但可访问静态属性。 2. 不能在对象实例中调用静态方法,因此在静态方法内部不可使用$this伪变量。
静态方法综合示例
<?php
class ShopProduct{
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
private $id = 0;
function __construct($title,$firstName,$mainName,$price){
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
}
public function setID($id){
$this->id = $id;
}
public static function getInstance($id,PDO $pdo){
$query = "select * from products where id= '$id'";
$stmt = $pdo->query($query);
$row = $stmt->fetch();
if(empty($row)){
return null;
}
if($row['type'] == "book"){
$product = new BookProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['numpages']
);
}else if($row['type'] == "cd"){
$product = new CdProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price'],
$row['playLength']
);
}else{
$product = new ShopProduct($row['title'],
$row['firstname'],
$row['mainname'],
$row['price']
);
}
$product->setId($row['id']);
$product->setDiscount($row['discount']);
return $product;
}
public function getProducerFirstName(){
return $this->producerFirstName;
}
public function getProducerMainName(){
return $this->producerMainName;
}
public function setDiscount($num){
$this->discount = $num;
}
public function getDiscount(){
return $this->discount;
}
public function getTitle(){
return $this->title;
}
public function getPrice(){
return ($this->price - $this->discount);
}
function getProducer(){
return $this->producerFirstName." ".$this->producerMainName;
}
function getSummaryLine(){
$base = "$this->title({$this->producerMainName},";
$base .= "{$this->producerFirstName})";
return $base;
}
}
class CdProduct extends ShopProduct{
private $playLength;
function __construct($title,$firstName,$mainName,$price,$playLength){
parent::__construct($title,$firstName,$mainName,$price);//继承父类的构造函数
$this->playLength = $playLength;
}
function getPlayLength(){
return $this->playLength;
}
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":playing time {$this->playLength}";
return $base;
}
}
class BookProduct extends ShopProduct{
private $numPages = 0;
function __construct($title,$firstName,$mainName,$price,$numPages){
parent::__construct($title,$firstName,$mainName,$price);
$this->numPages = $numPages;
}
function getnumPages(){
return $this->numPages;
}
function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ":page count {$this->numPages}";
return $base;
}
}
$dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db";
$pdo = new PDO($dsn,null,null);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$obj = ShopProduct::getInstance(1,$pdo);
echo $obj->getSummaryLine();
期望上述内容能对您的PHP程序设计工作提供有益帮助。