前端學PHP之物件導向系列第五篇——對象操作

來源:互聯網
上載者:User

標籤:恢複   引用   描述   lag   狀態   相等   轉換   位元組   page   

對象複製

  對象複製,又叫對象複製,可以通過 clone 關鍵字來完成

  在多數情況下,我們並不需要完全複製一個對象來獲得其中屬性。但有一個情況下確實需要:如果你有一個視窗對象,該對象持有視窗相關的資源。你可能會想複製一個新的視窗,保持所有屬性與原來的視窗相同,但必須是一個新的對象(因為如果不是新的對象,那麼一個視窗中的改變就會影響到另一個視窗)。還有一種情況:如果對象 A 中儲存著對象 B 的引用,當你複製對象A時,你想其中使用的對象不再是對象 B 而是 B 的一個副本,那麼你必須得到對象 A 的一個副本

<?php    class Person{        private $name;        private $sex;        private $age;        function __construct($name="",$sex="",$age=1){            $this->name= $name;            $this->sex = $sex;            $this->age = $age;        }        function say(){            echo "我的名字:" .$this->name.",性別:".$this->sex.",年齡:".$this->age."<br>";        }    }    $p1 = new Person(‘張三‘,‘男‘,‘20‘);    $p2 = clone $p1;    $p1->say();//我的名字:張三,性別:男,年齡:20    $p2->say();//我的名字:張三,性別:男,年齡:20?>

 

對象比較

  當使用比較子(==)比較兩個物件變數時,比較的原則是:如果兩個對象的屬性和屬性值都相等,而且兩個對象是同一個類的執行個體,那麼這兩個物件變數相等< $o = new Flag(); $p = new Flag();>

  而如果使用全等運算子(===),這兩個物件變數一定要指向某個類的同一個執行個體(即同一個對象)《$q = $o;》

<?phpfunction bool2str($bool){    if ($bool === false) {        return ‘FALSE‘;    } else {        return ‘TRUE‘;    }}function compareObjects(&$o1, &$o2){    echo ‘o1 == o2 : ‘ . bool2str($o1 == $o2) . "\n";    echo ‘o1 != o2 : ‘ . bool2str($o1 != $o2) . "\n";    echo ‘o1 === o2 : ‘ . bool2str($o1 === $o2) . "\n";    echo ‘o1 !== o2 : ‘ . bool2str($o1 !== $o2) . "\n";}class Flag{    public $flag;    function Flag($flag = true) {        $this->flag = $flag;    }}class OtherFlag{    public $flag;    function OtherFlag($flag = true) {        $this->flag = $flag;    }}$o = new Flag();$p = new Flag();$q = $o;//如果寫出 $q = clone $o的話 === 為fasle$r = new OtherFlag();/*Two instances of the same classo1 == o2 : TRUEo1 != o2 : FALSEo1 === o2 : FALSEo1 !== o2 : TRUE */echo "Two instances of the same class\n";compareObjects($o, $p);/*Two references to the same instanceo1 == o2 : TRUEo1 != o2 : FALSEo1 === o2 : TRUEo1 !== o2 : FALSE */echo "\nTwo references to the same instance\n";compareObjects($o, $q);/*Instances of two different classeso1 == o2 : FALSEo1 != o2 : TRUEo1 === o2 : FALSEo1 !== o2 : TRUE */echo "\nInstances of two different classes\n";compareObjects($o, $r);?>

 

對象序列化

  對象是一種在記憶體中儲存的資料類型,它的壽命通常隨著產生該對象的程式終止而終止。有時候可能需要將對象的狀態儲存下來,需要時再將對象恢複。對象通過寫出描述自己狀態的數值來記錄自己,這個過程稱對象的序列化(Serialization)。以下兩種情況需要將對象序列化:1、對象需要在網路中傳輸時,將對象序列化成二進位串即可;2、對象需要持久儲存時,將對象序列化後寫入檔案或資料庫

serialize()

  serialize() -- 序列化,返回一個包含位元組流的字串

unserialize()

  unserialize() -- 反序列化,能夠重新把字串變回php原來的對象值

  序列化一個對象將會儲存對象的所有屬性變數和類名資訊,但是不會儲存對象的方法

<?php// classa.inc:  class A {      public $one = 1;      public function show_one() {          echo $this->one;      }  }  // page1.php:  include("classa.inc");  $a = new A;  $s = serialize($a);  // 把變數$s儲存起來以便檔案page2.php能夠讀到  file_put_contents(‘store‘, $s);// page2.php:  include("classa.inc");  $s = file_get_contents(‘store‘);  $a = unserialize($s);  // 現在可以使用對象$a裡面的函數 show_one()  $a->show_one();?>

 

json

json_encode

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

  json_encode()方法對變數進行 JSON 編碼

<?php$arr = array (‘a‘=>1,‘b‘=>2,‘c‘=>3,‘d‘=>4,‘e‘=>5);echo json_encode($arr);//{"a":1,"b":2,"c":3,"d":4,"e":5}?>

json_decode

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

  json_decode()方法對 JSON 格式的字串進行解碼,接受一個 JSON 編碼的字串並且把它轉換為 PHP 變數,當assoc參數為 TRUE 時,將返回 array 而非 object

<?php$json = ‘{"a":1,"b":2,"c":3,"d":4,"e":5}‘;/*object(stdClass)#1 (5) {    ["a"] => int(1)    ["b"] => int(2)    ["c"] => int(3)    ["d"] => int(4)    ["e"] => int(5)} */var_dump(json_decode($json));/*array(5) {    ["a"] => int(1)    ["b"] => int(2)    ["c"] => int(3)    ["d"] => int(4)    ["e"] => int(5)} */var_dump(json_decode($json, true));?>

前端學PHP之物件導向系列第五篇——對象操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.