1.php与对象

知识点:

a.关于引用赋值

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

例子:

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

2.类与对象

知识点

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

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

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

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

b.一个类实例

<?php
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 getProducer(){
return $this->producerFirstName." ".$this->producerMainName;
}
}
$product1 = new ShopProduct("My pro","Willa","Tom",5.99);
print "author:{$product1->getProducer()}\n";

输出:

author:Willa Tom

一个更复杂的例子

<?php
class CdProduct{
public $playLength;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title,$firstName,$mainName,$price,$playLength){
$this->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()}<br/>"; $product2 = new CdProduct("My pro","Willa","Tom",5.99,1);
print "PlayLength:{$product2->getPlayLength()}<br/>"; $product3 = new BookProduct("My pro","Willa","Tom",5.99,10);
print "numPages:{$product3->getnumPages()}<br/>";

结果:

author:Willa Tom
PlayLength:1
numPages:10

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

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

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

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

下面是继承的案例:

<?php
class ShopProduct{
public $numPages;
public $playLength;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title,$firstName,$mainName,$price,$numPages=0,$playLength=0){
$this->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()}<br/>"; $product2 = new CdProduct("My pro","Willa","Tom",5.99,null,5);
print "SummaryLine:{$product2->getSummaryLine()}<br/>"; $product3 = new BookProduct("My pro","Willa","Tom",5.99,10,null);
print "SummaryLine:{$product3->getSummaryLine()}<br/>";

结果:

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

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

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

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

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

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

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

<?php
class ShopProduct{
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
function __construct($title,$firstName,$mainName,$price){
$this->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()}<br/>"; $product2 = new CdProduct("My pro","Willa","Tom",5.99,5);
print "SummaryLine:{$product2->getSummaryLine()}<br/>"; $product3 = new BookProduct("My pro","Willa","Tom",5.99,10);
print "SummaryLine:{$product3->getSummaryLine()}<br/>";

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

《PHP对象、模式与实践》之对象的更多相关文章

  1. Python 源码剖析(一)【python对象】

    处于研究python内存释放问题,在阅读部分python源码,顺便记录下所得.(基于<python源码剖析>(v2.4.1)与 python源码(v2.7.6)) 先列下总结:      ...

  2. Python源码剖析——01内建对象

    <Python源码剖析>笔记 第一章:对象初识 对象是Python中的核心概念,面向对象中的"类"和"对象"在Python中的概念都为对象,具体分为 ...

  3. python源码剖析学习记录-01

    学习<Python源码剖析-深度探索动态语言核心技术>教程         Python总体架构,运行流程   File Group: 1.Core Modules 内部模块,例如:imp ...

  4. Python源码剖析|百度网盘免费下载|Python新手入门|Python新手学习资料

    百度网盘免费下载:Python源码剖析|新手免费领取下载 提取码:g78z 目录  · · · · · · 第0章 Python源码剖析——编译Python0.1 Python总体架构0.2 Pyth ...

  5. Python源码剖析——02虚拟机

    <Python源码剖析>笔记 第七章:编译结果 1.大概过程 运行一个Python程序会经历以下几个步骤: 由解释器对源文件(.py)进行编译,得到字节码(.pyc文件) 然后由虚拟机按照 ...

  6. Python 源码剖析 目录

    Python 源码剖析 作者: 陈儒 阅读者:春生 版本:python2.5 版本 本博客园的博客记录我会适当改成Python3版本 阅读 Python 源码剖析 对读者知识储备 1.C语言基础知识, ...

  7. Python 源码剖析(六)【内存管理机制】

    六.内存管理机制 1.内存管理架构 2.小块空间的内存池 3.循环引用的垃圾收集 4.python中的垃圾收集 1.内存管理架构 Python内存管理机制有两套实现,由编译符号PYMALLOC_DEB ...

  8. 【Python源码剖析】对象模型概述

    Python 是一门 面向对象 语言,实现了一个完整的面向对象体系,简洁而优雅. 与其他面向对象编程语言相比, Python 有自己独特的一面. 这让很多开发人员在学习 Python 时,多少有些无所 ...

  9. [Python源码剖析]字符缓冲池intern机制

    static PyStringObject *characters[UCHAR_MAX + 1]; ... /* This dictionary holds all interned strings. ...

  10. [Python源码剖析]获取Python小整数集合范围

    #!/usr/bin/env python #-*- coding=utf-8 -*- small_ints = dict() for i in range(-10000,10000): small_ ...

随机推荐

  1. 160808、Java的不同版本:J2SE、J2EE、J2ME的区别

    来源:微学苑 在Java中,同一个类中的多个方法可以有相同的名字,只要它们的参数列表不同就可以,这被称为方法重载(method overloading). 参数列表又叫参数签名,包括参数的类型.参数的 ...

  2. 导出Excel功能的3种实现

    项目中总会用到Excel的导出功能,接触过好几个项目,发现有个项目的导出实现特别值得学习.这里学习顺带总结一下. 一.三种方法 我遇到的导出目前有3种处理: 每个功能一个导出方法: 写一个通用的Exp ...

  3. 『浅入浅出』MySQL 和 InnoDB

    作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...

  4. Sublime text找不到.so文件

    在使用Sublime text打开一个android项目的时候,你会发现找不到.so文件. 解决方法: 点击Sublime text的Preferences,然后点击Settings,这时候出现设置的 ...

  5. 【Python之路】第二十三篇--Django【进阶篇】

    文件配置 1.模版Templates文件配置: TEMPLATE_DIRS = ( os.path.join(BASE_DIR,'templates'), ) 2.静态文件static配置: STAT ...

  6. php 解决上传中文文件名时出现乱码的问题

    有时候上传文件是中文的文件名会出现乱码, 可以在移动文件时使用icov('utf-8','gb2312',filename)转换 代码: <?php //header('Content-type ...

  7. Design Pattern - 命令模式

    一般执行一个操作的过程, 创建对象, 并调用对象的函数, 函数执行, 返回 比如下面的类图, client直接调用Receiver.action 而命令模式, 抽象出command对象, 并在comm ...

  8. HTTP Keep-Alive是什么?如何工作?(转)

    add by zhj: 本篇只是Keep-Alive的第一篇,其它文章参见下面的列表. 原文: HTTP Keep-Alive是什么?如何工作? 1. HTTP Keep-Alive是什么?如何工作? ...

  9. java.lang.instrument: 一个Java对象占用多少字节?

    一.对象头包括两部分信息:Mark Word(标记字段)和 Klass Pointer(类型指针)   1. Mark Word 用于存储对象自身的运行时数据,如哈希码(HashCode).GC分代年 ...

  10. c++ 11nullptr

    1. 引入nullptr的原因 引入nullptr的原因,这个要从NULL说起.对于C和C++程序员来说,一定不会对NULL感到陌生.但是C和C++中的NULL却不等价.NULL表示指针不指向任何对象 ...