本文实例讲述了php面向对象程序设计高级特性。分享给大家供大家参考,具体如下:
高级特性
包括:
1.静态方法和属性(通过类而不是对象来访问数据和功能)
2.抽象类和接口(设计,实现分离)
3.错误处理(异常)
4.final类和方法(限制继承)
5.拦截器(自动委托)
6.析构方法(对象销毁前的清理工作)
7.克隆对象(创建对象的副本)
8.把对象解析成字符串
ps,学会从内存的角度看代码。想象计算机的微观世界。
静态方法的小例子
<?php
class staticexample{
static public $anum = 10;
static public function sayhello(){
print "hello";
}
}
print staticexample::$anum."<br/>";
staticexample::sayhello();
tips:
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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
评论列表:
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论