首頁 > 後端開發 > php教程 > 行動app介面程式設計技術-學習實現之PHP類其他特性

行動app介面程式設計技術-學習實現之PHP類其他特性

WBOY
發布: 2016-07-30 13:31:23
原創
1018 人瀏覽過

  • Static靜態關鍵字

    靜態。 在類別中,被他標記的變數或方法,不屬於任何一個物件。訪問的時候要使用“::“。並且在類別中自我調用的時候要是用”self::”
    例如:

<code><span><span><?php</span><span><span>class</span><span>Car</span> {</span><span>private</span><span>static</span><span>$speed</span> = <span>10</span>;

    <span>public</span><span><span>function</span><span>getSpeed</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$speed</span>;
    }

    <span>//在这里定义一个静态方法,实现速度累加10</span><span>public</span><span>static</span><span><span>function</span><span>speedUp</span><span>()</span>
    {</span><span>return</span><span>self</span>::<span>$speed</span> += <span>10</span>;
    }
}

<span>$car</span> = <span>new</span> Car();
Car::speedUp();  <span>//调用静态方法加速</span><span>echo</span><span>$car</span>->getSpeed();  <span>//调用共有方法输出当前的速度值</span></span></span></code>
登入後複製

靜態方法也可以透過變數來進行動態呼叫。

<code><span>$func</span> = <span>'getSpeed'</span>;
<span>$className</span> = <span>'Car'</span>;
<span>echo</span><span>$className</span>::<span>$func</span>();  <span>//动态调用静态方法</span></code>
登入後複製
  • 存取控制

存取控制透過關鍵字public,protected和private來實現。被定義為公有的類別成員可以在任何地方被存取。被定義為受保護的類別成員則可以被其本身以及其子類別和父類別存取。被定義為私有的類別成員則只能被其定義所在的類別存取。

類屬性必須定義為公有、受保護、私有之一。

類別中的方法可以被定義為公有、私有或受保護。如果沒有設定這些關鍵字,則該方法預設為公有。

如果建構函式定義成了私有方法,則不允許直接實例化物件了,這時候一般透過靜態方法進行實例化,在設計模式中會經常使用這樣的方法來控制物件的創建,例如單例模式只允許有一個全域唯一的物件。

<code><span><span>class</span><span>Car</span> {</span><span>private</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>echo</span><span>'object create'</span>;
    }

    <span>private</span><span>static</span><span>$_object</span> = <span>null</span>;
    <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>if</span> (<span>empty</span>(<span>self</span>::<span>$_object</span>)) {
            <span>self</span>::<span>$_object</span> = <span>new</span> Car(); 
            <span>//内部方法可以调用私有方法,因此这里可以创建对象</span>
        }
        <span>return</span><span>self</span>::<span>$_object</span>;
    }
}
<span>//$car = new Car(); //这里不允许直接实例化对象</span><span>$car</span> = Car::getInstance(); <span>//通过静态方法来获得一个实例</span></code>
登入後複製
  • 繼承
<code><span><span><?php</span><span><span>class</span><span>Car</span> {</span><span>public</span><span>$speed</span> = <span>0</span>; <span>//汽车的起始速度是0</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed += <span>10</span>;
        <span>return</span><span>$this</span>->speed;
    }
}
<span>//定义继承于Car的Truck类</span><span><span>class</span><span>Truck</span><span>extends</span><span>Car</span>{</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed = <span>parent</span>::speedUp() + <span>50</span>;
    }
}

<span>$car</span> = <span>new</span> Truck();
<span>$car</span>->speedUp();
<span>echo</span><span>$car</span>->speed;</span></span></code>
登入後複製
  • 重載

PHP中的重載指的是動態的創建屬性與方法,是透過魔術方法來實現的。屬性的重載透過__set,__get,__isset,__unset來分別實現對不存在屬性的賦值、讀取、判斷屬性是否設定、銷毀屬性。

<code><span><span>class</span><span>Car</span> {</span><span>private</span><span>$ary</span> = <span>array</span>();

    <span>public</span><span><span>function</span><span>__set</span><span>(<span>$key</span>, <span>$val</span>)</span> {</span><span>$this</span>->ary[<span>$key</span>] = <span>$val</span>;
    }

    <span>public</span><span><span>function</span><span>__get</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) {
            <span>return</span><span>$this</span>->ary[<span>$key</span>];
        }
        <span>return</span><span>null</span>;
    }

    <span>public</span><span><span>function</span><span>__isset</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) {
            <span>return</span><span>true</span>;
        }
        <span>return</span><span>false</span>;
    }

    <span>public</span><span><span>function</span><span>__unset</span><span>(<span>$key</span>)</span> {</span><span>unset</span>(<span>$this</span>->ary[<span>$key</span>]);
    }
}
<span>$car</span> = <span>new</span> Car();
<span>$car</span>->name = <span>'汽车'</span>;  <span>//name属性动态创建并赋值</span><span>echo</span><span>$car</span>->name;
</code>
登入後複製

方法的重載是透過__call來實現,當呼叫不存在的方法的時候,將會轉為參數呼叫__call方法,當呼叫不存在的靜態方法時會使用__callStatic重載。

<code>lass Car {
    <span>public</span><span>$speed</span> = <span>0</span>;

    <span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span>, <span>$args</span>)</span> {</span><span>if</span> (<span>$name</span> == <span>'speedUp'</span>) {
            <span>$this</span>->speed += <span>10</span>;
        }
    }
}
<span>$car</span> = <span>new</span> Car();
<span>$car</span>->speedUp(); <span>//调用不存在的方法会使用重载</span><span>echo</span><span>$car</span>->speed;</code>
登入後複製
  • 類別物件比較

物件比較,當同一個類別的兩個實例的所有屬性都相等時,可以使用比較運算子「==「進行判斷,當需要判斷兩個變數是否為同一個物件的參考時,可以使用全等運算子「===「進行判斷。

<code><span><span>class</span><span>Car</span> {</span>
}
<span>$a</span> = <span>new</span> Car();
<span>$b</span> = <span>new</span> Car();
<span>if</span> (<span>$a</span> == <span>$b</span>) <span>echo</span><span>'=='</span>;   <span>//true</span><span>if</span> (<span>$a</span> === <span>$b</span>) <span>echo</span><span>'==='</span>; <span>//false</span></code>
登入後複製

對象複製,在一些特殊情況下,可以透過關鍵字clone來複製一個對象,這時__clone方法會被調用,透過這個魔術方法來設定屬性的值。

<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>;

    <span>public</span><span><span>function</span><span>__clone</span><span>()</span> {</span><span>$obj</span> = <span>new</span> Car();
        <span>$obj</span>->name = <span>$this</span>->name;
    }
}
<span>$a</span> = <span>new</span> Car();
<span>$a</span>->name = <span>'new car'</span>;
<span>$b</span> = <span>clone</span><span>$a</span>;
var_dump(<span>$b</span>);
</code>
登入後複製

物件序列化,可以透過serialize方法將物件序列化為字串,用於儲存或傳遞數據,然後在需要的時候透過unserialize將字串反序列化成物件進行使用。

<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>;
}
<span>$a</span> = <span>new</span> Car();
<span>$str</span> = serialize(<span>$a</span>); <span>//对象序列化成字符串</span><span>echo</span><span>$str</span>.<span>'<br>'</span>;
<span>$b</span> = unserialize(<span>$str</span>); <span>//反序列化为对象</span>
var_dump(<span>$b</span>);
</code>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了行動app介面程式設計技術-學習實作之PHP類其他特性,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板