《PHP对象、模式与实践》之对象

简介:

1.php与对象

知识点:

a.关于引用赋值

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

例子:

复制代码
<?php
$my_obj = 1;
echo $my_obj."<br/>";//
$other = &$my_obj;
echo $other."<br/>";//1
$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/>";
复制代码

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



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


相关文章
|
2月前
|
程序员 PHP
PHP程序员的成长之路:技术探索与实践
在当今数字化时代,PHP作为一种广泛应用的后端编程语言,对于程序员而言具有重要意义。本文从技术探索和实践的角度出发,探讨了PHP程序员在成长过程中所面临的挑战与机遇,以及如何通过持续学习与实践不断提升自身技能。
|
2月前
|
Java 程序员 PHP
PHP对象和类
PHP对象和类
22 0
|
2天前
|
PHP
PHP 7.4中新增特性的探索与实践
【5月更文挑战第12天】本文主要探讨了PHP 7.4中的一些新特性,包括箭头函数、预加载优化、数组表达式间接访问等。通过对这些新特性的深入理解和实践应用,可以帮助我们编写出更高效、更简洁的代码。
|
8天前
|
存储 缓存 自然语言处理
深入PHP内核:理解OPcache的工作原理与优化实践
【5月更文挑战第6天】 在现代Web开发中,提升性能和响应速度是持续追求的目标。PHP作为一种广泛使用的服务端脚本语言,其执行效率至关重要。本文将深入探索PHP的OPcache(优化器缓存)组件,解析其如何改善PHP的性能表现。通过剖析OPcache的工作机制,我们将讨论有效的配置策略以及实践中的最佳优化方法,旨在帮助开发者充分理解并利用OPcache来提升应用性能。
|
14天前
|
PHP 开发者
PHP中的命名空间深入理解与实践
【4月更文挑战第30天】在现代PHP开发中,命名空间是管理代码中类名和函数名冲突的重要工具。本文将探讨PHP命名空间的核心概念、实现机制及其在实际项目中的应用场景,帮助开发者更有效地组织和维护大型项目代码。我们将通过实例分析如何利用命名空间避免常见的名称冲突问题,并提供最佳实践建议。文章旨在为有一定PHP基础的开发人员提供进阶指导,使其能够在复杂的项目结构中灵活运用命名空间。
|
15天前
|
设计模式 算法 搜索推荐
【PHP开发专栏】PHP设计模式解析与实践
【4月更文挑战第29天】本文介绍了设计模式在PHP开发中的应用,包括创建型(如单例、工厂模式)、结构型和行为型模式(如观察者、策略模式)。通过示例展示了如何在PHP中实现这些模式,强调了它们在提升代码可维护性和可扩展性方面的作用。设计模式是解决常见问题的最佳实践,但在使用时需避免过度设计,根据实际需求选择合适的设计模式。
|
15天前
|
编译器 PHP
深入理解PHP 8.0的新特性及实践应用
【4月更文挑战第29天】在这篇文章中,我们将深入探讨PHP 8.0的新特性及其在实际开发中的应用。通过对新特性的详细解析,我们将了解到PHP 8.0如何提高开发效率,优化代码质量,以及提升应用程序的性能。同时,我们还将通过实际案例,展示如何在项目中应用这些新特性,以实现更高效、更稳定的开发环境。
|
20天前
|
缓存 安全 JavaScript
PHP 7.4新特性解析与实践
【4月更文挑战第24天】 在这篇文章中,我们将深入探讨PHP 7.4版本的新特性,并通过实际示例代码展示如何将这些新特性应用于日常开发工作中。我们将重点介绍预加载优化、类型化属性、箭头函数等重要更新,并分析这些新特性对性能和编码习惯的影响。通过本文,读者将获得对PHP 7.4新特性的全面理解,以及如何有效地利用这些新工具来提升代码质量和开发效率。
|
1月前
|
PHP
PHP 7.4的新特性及实践应用
【4月更文挑战第2天】本文主要介绍了PHP 7.4的新特性,并通过实例代码展示了如何在实际项目中应用这些新特性。文章首先简要回顾了PHP的发展历史,然后详细分析了PHP 7.4的新特性,包括预加载、数组解构、扩展的返回类型声明等。接下来,通过实际代码示例,演示了如何在项目中使用这些新特性。最后,总结了PHP 7.4新特性的优势和局限性,并展望了PHP未来的发展趋势。
|
2月前
|
安全 大数据 编译器
深入理解PHP 8.0的新特性及实践应用
【2月更文挑战第30天】随着PHP 8.0的发布,这一流行的服务器端脚本语言带来了许多令人兴奋的新特性和性能改进。本文将深入探讨PHP 8.0的关键新特性,包括JIT编译器、联合类型、名称参数、匹配表达式等,并通过实际代码示例展示如何利用这些新工具来编写更加高效、简洁和健壮的应用程序。无论您是PHP开发者还是对最新技术趋势感兴趣的技术爱好者,本文都将为您提供宝贵的信息和启发。
26 3