博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《PHP对象、模式与实践》之对象
阅读量:6824 次
发布时间:2019-06-26

本文共 9150 字,大约阅读时间需要 30 分钟。

1.php与对象

知识点:

a.关于引用赋值

other = &other = &my_obj;

//按照引用复制,指向相同对象。
例子:

";//1$other = &$my_obj;echo $other."
";//1$my_obj = 2;echo $other;//2//按照引用复制,指向相同对象。

 

2.类与对象

知识点

a.类是对象的模板,对象是类实现的实例

变量函数对应类中的属性和方法。

和函数不同的是,方法必须在类体中声明。

$this是伪变量,可以将类指向一个对象实例。

 

b.一个类实例

title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; }}$product1 = new ShopProduct("My pro","Willa","Tom",5.99);print "author:{
$product1->getProducer()}\n";

输出:

author:Willa Tom

 

一个更复杂的例子

title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; $this->playLength = $playLength; } function getPlayLength(){ return $this->playLength; } function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":playing time -{
$this->playLength}"; return $base; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; }}class BookProduct{ public $numPages; public $title; public $producerMainName; public $producerFirstName; public $price; function __construct($title,$firstName,$mainName,$price,$numPages){ $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; $this->numPages = $numPages; } function getnumPages(){ return $this->numPages; } function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":page count -{
$this->numPages}"; return $base; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; }}class ShopProduct{ public $title; public $producerMainName; public $producerFirstName; public $price = 0; function __construct($title,$firstName,$mainName,$price){ $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; }   function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; return $base; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; }}$product1 = new ShopProduct("My pro","Willa","Tom",5.99);print "author:{
$product1->getProducer()}
";$product2 = new CdProduct("My pro","Willa","Tom",5.99,1);print "PlayLength:{
$product2->getPlayLength()}
";$product3 = new BookProduct("My pro","Willa","Tom",5.99,10);print "numPages:{
$product3->getnumPages()}
";

结果:

author:Willa Tom

PlayLength:1
numPages:10

点评:这三个类写在同一个文件下面,说明php支持一个文件包含多个类。只是这样有点不太好,最好单独一个文件,把他们引入进来,然后创建对象,使用。

这三个类还有一个缺点就是,代码重复了,每个类中都有getSummaryLine()方法,和getProducer()方法。这样就冗余了,这个时候怎么办呢?

如果类之间有一定的继承关系,可以用继承这种机制,当然也不要继承很多层次,那样太深了也不好。

适当的继承能够让类更简洁,更利索!

 

下面是继承的案例:

title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; $this->numPages = $numPages; $this->playLength = $playLength; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; } function getSummaryLine(){ $base = "$this->title({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; return $base; }}class CdProduct extends ShopProduct{ function getPlayLength(){ return $this->playLength; } function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":playing time {
$this->playLength}"; return $base; }}class BookProduct extends ShopProduct{ function getnumPages(){ return $this->numPages; } function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":page count {
$this->numPages}"; return $base; }}$product1 = new ShopProduct("My pro","Willa","Tom",5.99);print "SummaryLine:{
$product1->getSummaryLine()}
";$product2 = new CdProduct("My pro","Willa","Tom",5.99,null,5);print "SummaryLine:{
$product2->getSummaryLine()}
";$product3 = new BookProduct("My pro","Willa","Tom",5.99,10,null);print "SummaryLine:{
$product3->getSummaryLine()}
";

结果:

SummaryLine:My pro(Tom,Willa)

SummaryLine:My pro(Tom,Willa):playing time 5
SummaryLine:My pro(Tom,Willa):page count 10

点评:子类继承父类的属性和构造行数,以及一些基本的函数。

继承之后,可以覆盖父类的函数,也可以新建自己的函数。继承可以避免类内容的重复,代码的重复。

 

继续改造,子类中也有自己的构造方法。

在子类中定义构造方法时,需要传递参数给父类的构造方法,否则你得到的可能是一个构造不完整的对象。

title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } function getProducer(){ return $this->producerFirstName." ".$this->producerMainName; } function getSummaryLine(){ $base = "$this->title({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; return $base; }}class CdProduct extends ShopProduct{ public $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 = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":playing time {
$this->playLength}"; return $base; }}class BookProduct extends ShopProduct{ public $numPages; function __construct($title,$firstName,$mainName,$price,$numPages){ parent::__construct($title,$firstName,$mainName,$price); $this->numPages = $numPages; } function getnumPages(){ return $this->numPages; } function getSummaryLine(){ $base = "{
$this->title}({
$this->producerMainName},"; $base .= "{
$this->producerFirstName})"; $base .= ":page count {
$this->numPages}"; return $base; }}$product1 = new ShopProduct("My pro","Willa","Tom",5.99);print "SummaryLine:{
$product1->getSummaryLine()}
";$product2 = new CdProduct("My pro","Willa","Tom",5.99,5);print "SummaryLine:{
$product2->getSummaryLine()}
";$product3 = new BookProduct("My pro","Willa","Tom",5.99,10);print "SummaryLine:{
$product3->getSummaryLine()}
";

结果同上一个效果一点,这里面每个子类都有自己的构造方法了,同时继承了父类中的构造方法。这样就保证了子类的灵活性。不完全受制于父类。

 

进一步添加访问权限设置,

title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } 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; }}$product1 = new ShopProduct("My pro","Willa","Tom",5.99);print "SummaryLine:{
$product1->getSummaryLine()}
";$product2 = new CdProduct("My pro","Willa","Tom",5.99,5);print "SummaryLine:{
$product2->getSummaryLine()}
";$product3 = new BookProduct("My pro","Willa","Tom",5.99,10);print "SummaryLine:{
$product3->getSummaryLine()}
";

点评:一般属性设置为私有的,只能通过方法来设置和获取,这样能保证安全性。

本文转自TBHacker博客园博客,原文链接http://www.cnblogs.com/jiqing9006/p/3178339.html,如需转载请自行联系原作者

你可能感兴趣的文章
20.31 expect脚本同步文件;20.32 expect脚本指定host和要同步的文件;20.33 构建文件分发系统;20.34...
查看>>
CentOS单用户与救援模式
查看>>
postfix 源码centos7上搭建及错误提示---亲测
查看>>
【Redis篇】Redis集群安装与初始
查看>>
jquery基础
查看>>
C# 集合已修改;可能无法执行枚举操作
查看>>
FSM Code Generator
查看>>
JDBC学习笔记——事务、存储过程以及批量处理
查看>>
JVM内存结构
查看>>
Java 锁
查看>>
7、索引在什么情况下遵循最左前缀的规则?
查看>>
c#中委托与事件
查看>>
mysql数据库备份之主从同步配置
查看>>
angularJs(1)指令篇
查看>>
自定义Xadmin
查看>>
jsp页面表单的遍历要怎么写
查看>>
循环引用,看我就对了
查看>>
软件工程——第一周作业
查看>>
ubuntu14.04安装vmware workstation
查看>>
ArcGIS API for Silverlight部署本地地图服务
查看>>