『PHP対象、パターンと実践』の対象

40782 ワード

1.phpとオブジェクト
知識点:
a.参照割付について
$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()}
";

出力:
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 TomPlayLength:1numPages:10
コメント:この3つのクラスは同じファイルの下に書かれており、phpが1つのファイルに複数のクラスを含むことをサポートしていることを示しています.ただ、これはちょっとよくありません.単独のファイルを導入して、オブジェクトを作成して使用したほうがいいです.
この3つのクラスには、コードが重複しており、各クラスに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 5SummaryLine: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/>";

コメント:一般的な属性はプライベートに設定されており、方法で設定して取得するしかなく、安全性を保証することができます.