欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          帶你吃透16個PHP魔術(shù)方法

          什么是魔術(shù)方法?本篇文章帶大家了解一下16個PHP 開發(fā)者必知必會的魔術(shù)方法,希望對大家有所幫助!

          帶你吃透16個PHP魔術(shù)方法

          在PHP中,以雙下劃線(__)開始命名的方法被稱作PHP中的魔術(shù)方法,它們在PHP中充當(dāng)很重要的角色。魔術(shù)方法包括:

          方法名 描述
          __construct() 類的構(gòu)造函數(shù)
          __destruct() 類的析構(gòu)函數(shù)
          __call($funName, $arguments) 當(dāng)調(diào)用一個未定義或不可達(dá)方法時(shí), __call() 方法將被調(diào)用。
          __callStatic($funName, $arguments) 當(dāng)調(diào)用一個未定義或不可達(dá)的靜態(tài)方法時(shí), __callStatic() 方法將被調(diào)用。
          __get($propertyName) 當(dāng)獲取一個類的成員變量時(shí), __get() 方法將被調(diào)用。
          __set($property, $value) 當(dāng)賦值一個類的成員變量時(shí), __set() 方法將被調(diào)用。
          __isset($content) 當(dāng)調(diào)用 isset() 或 empty() 對一個未定義或不可達(dá)的成員賦值時(shí), __isset() 方法將被調(diào)用。
          __unset($content) 當(dāng)調(diào)用 reset() 對一個未定義或不可達(dá)的成員更新時(shí), __unset() 方法將被調(diào)用。
          __sleep() 當(dāng)執(zhí)行序列化 serialize() 時(shí),__sleep() 方法將首先被調(diào)用。
          __wakeup() 當(dāng)執(zhí)行反序列化 deserialization() 時(shí), __wakeup() 方法將首先被調(diào)用。
          __toString() 當(dāng)使用 echo 方法直接輸出顯示對象時(shí),__toString() 方法首先被調(diào)用。
          __invoke() 使用調(diào)用函數(shù)(function)訪問一個對象時(shí), __invoke() 方法將首先被調(diào)用。
          __set_state($an_array) 當(dāng)調(diào)用 var_export() 方法時(shí),__set_state() 方法將被調(diào)用。
          __clone() 當(dāng)對象被復(fù)制賦值時(shí),__clone() 方法將被調(diào)用。
          __autoload($className) 試圖載入一個未定義的類時(shí)調(diào)用。
          __debugInfo() 輸出 debug 信息。

          本文將使用一些實(shí)例展示 PHP 魔術(shù)方法的運(yùn)用。

          1.__construct()

          當(dāng)創(chuàng)建對象時(shí),PHP 類的構(gòu)造方法是第一個被調(diào)用的方法。每個類都有構(gòu)造方法。若你沒在類中明確聲明定義它,將會有一個默認(rèn)的無參類構(gòu)造方法存在,雖然它不會在類中定義出現(xiàn)。

          1) 構(gòu)造方法的運(yùn)用

          類的構(gòu)造方法通常用于執(zhí)行一些初始化任務(wù),諸如當(dāng)創(chuàng)建對象時(shí),為成員初始化賦值。

          2) 類中構(gòu)造方法的聲明格式

          function __constrct([parameter list]){      方法具體實(shí)現(xiàn) //通常為成員變量初始賦值。  }

          注意: 在多數(shù)類中僅可以聲明一個構(gòu)造方法。因?yàn)椋?PHP 不支持構(gòu)造方法重載。

          下面是個完整的例子:

          <?php     class Person     {                                                                                  public $name;                    public $age;                    public $sex;                 /**          * 明確定義含參的構(gòu)造方法          */                                                                                                public function __construct($name="", $sex="Male", $age=22)         {                  $this->name = $name;             $this->sex = $sex;             $this->age = $age;         }          /**          * say 方法定義          */         public function say()         {             echo "Name:" . $this->name . ",Sex:" . $this->sex . ",Age:" . $this->age;         }         }

          無參創(chuàng)建 $Person1 對象。

          $Person1 = new Person(); echo $Person1->say(); //顯示:Name:,Sex:Male,Age:22

          使用一個參數(shù) "Jams" 調(diào)用創(chuàng)建 $Person2 對象。

          $Person2 = new Person("Jams"); echo $Person2->say(); // 顯示: Name: Jams, Sex: Male, Age: 22

          使用3個參數(shù)調(diào)用創(chuàng)建 $Person3 對象。

          $Person3 = new Person ("Jack", "Male", 25); echo $Person3->say(); // 顯示:Name: Jack, Sex: Male, Age: 25

          __destruct()

          析構(gòu)函數(shù)與構(gòu)造函數(shù)相反。

          析構(gòu)函數(shù)允許你在銷毀對象之前執(zhí)行一些操作,例如關(guān)閉文件,清空結(jié)果集等等。

          析構(gòu)函數(shù)是 PHP 5 引入的新功能。

          析構(gòu)函數(shù)的聲明與構(gòu)造函數(shù)類似,以兩個下劃線開頭,名稱固定為 __destruct()

          析構(gòu)函數(shù)的聲明

          function __destruct() {     //method body }

          析構(gòu)函數(shù)不能帶參數(shù)。

          析構(gòu)函數(shù)的使用

          析構(gòu)函數(shù)在類中一般不常見。它是類的可選部分,通常用于在類銷毀之前完成一些清理任務(wù)。

          這是使用析構(gòu)函數(shù)的示例:

          <?php class Person{           public $name;              public $age;              public $sex;               public function __construct($name="", $sex="Male", $age=22)     {            $this->name = $name;         $this->sex  = $sex;         $this->age  = $age;     }      /**      * say method      */     public function say()     {         echo "Name:".$this->name.",Sex:".$this->sex.",Age:".$this->age;     }         /**      * declare a destructor method      */     public function __destruct()     {             echo "Well, my name is ".$this->name;     } }  $Person = new Person("John"); unset($Person); //destroy the object of $Person created above

          輸出結(jié)果

          Well, my name is John

          __call()

          該方法接受兩個參數(shù)。第一個參數(shù)為未定義的方法名稱,第二個參數(shù)則為傳入方法的參數(shù)構(gòu)成的數(shù)組

          使用

          function __call(string $function_name, array $arguments) {     // method body }

          在程序中調(diào)用未定義方法時(shí), __call() 方法將被調(diào)用。

          示例

          <?php class Person {                                  function say()     {            echo "Hello, world!<br>";     }           function __call($funName, $arguments)     {           echo "The function you called:" . $funName . "(parameter:" ;  // Print the method's name that is not existed.           print_r($arguments); // Print the parameter list of the method that is not existed.           echo ")does not exist!!<br>n";                        }                                          } $Person = new Person();            $Person->run("teacher"); // If the method which is not existed is called within the object, then the __call() method will be called automatically. $Person->eat("John", "apple");              $Person->say();

          顯示結(jié)果

          The function you called: run (parameter: Array([0] => teacher)) does not exist! The function you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!

          4. __callStatic()

          當(dāng)在程序中調(diào)用未定義的靜態(tài)方法,__callStatic() 方法將會被自動調(diào)用。

          __callStatic() 的用法類似于 __call() 。下面舉個例子:

          <?php class Person {     function say()     {         echo "Hello, world!<br>";     }      public static function __callStatic($funName, $arguments)     {         echo "The static method you called:" . $funName . "(parameter:" ;  // 打印出未定義的方法名。         print_r($arguments); // 打印出未定義方法的參數(shù)列表。         echo ")does not exist!<br>n";     } } $Person = new Person(); $Person::run("teacher"); // 如果此項(xiàng)目內(nèi)不存在的方法被調(diào)用了,那么 __callStatic() 方法將被自動調(diào)用。 $Person::eat("John", "apple"); $Person->say();

          執(zhí)行結(jié)果如下:

          The static method you called: run (parameter: Array([0] => teacher)) does not exist! The static method you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!

          __get()

          當(dāng)你嘗試在外部訪問對象的私有屬性時(shí),應(yīng)用程序?qū)伋霎惓2⒔Y(jié)束運(yùn)行。我們可以使用 __get 方法解決該問題。該方法可以獲取從對象外部獲取私有屬性的值。舉例如下

          <?php class Person {     private $name;     private $age;      function __construct($name="", $age=1)     {         $this->name = $name;         $this->age = $age;     }      public function __get($propertyName)     {            if ($propertyName == "age") {             if ($this->age > 30) {                 return $this->age - 10;             } else {                 return $this->$propertyName;             }         } else {             return $this->$propertyName;         }     } } $Person = new Person("John", 60);   // Instantiate the object with the Person class and assign initial values to the properties with the constructor. echo "Name:" . $Person->name . "<br>";   // When the private property is accessed, the __get() method will be called automatically,so we can get the property value indirectly. echo "Age:" . $Person->age . "<br>";    // The __get() method is called automatically,and it returns different values according to the object itself.

          結(jié)果顯示如下

          Name: John Age: 50

          6. __set()

          set($property,$value)方法用于設(shè)置類的私有屬性。分配了未定義的屬性后,將觸發(fā)set()方法,并且傳遞的參數(shù)是設(shè)置的屬性名稱和值。

          下面是演示代碼:

          <?php class Person {     private $name;     private $age;      public function __construct($name="",  $age=25)     {         $this->name = $name;         $this->age  = $age;     }      public function __set($property, $value) {         if ($property=="age")         {             if ($value > 150 || $value < 0) {                 return;             }         }         $this->$property = $value;     }      public function say(){         echo "My name is ".$this->name.",I'm ".$this->age." years old";     } }  $Person=new Person("John", 25); //請注意,類初始化并為“name”和“age”分配初始值。 $Person->name = "Lili";     // "name" 屬性值被成功修改。如果沒有__set()方法,程序?qū)?bào)錯。 $Person->age = 16; // "age"屬性修改成功。 $Person->age = 160; //160是無效值,因此修改失敗。 $Person->say();  //輸出:My name is Lili, I'm 16 years old。

          代碼運(yùn)行結(jié)果:

          My name is Lili, I'm 16 years old

          7. __isset()

          在使用__isset()方法之前,讓我先解釋一下isset()方法的用法。isset()方法主要用于確定是否設(shè)置了此變量。

          如果在對象外部使用isset()方法,則有兩種情況:

          1. 如果該參數(shù)是公共屬性,則可以使用isset()方法確定是否設(shè)置了該屬性。
          2. 如果參數(shù)是私有屬性,則isset()方法將不起作用。

          那么對于私有屬性,有什么辦法知道它是否被設(shè)置了嗎?當(dāng)然,只要在類中定義__isset()方法,就可以在類外部使用isset()方法來確定是否設(shè)置了私有屬性。

          當(dāng)在未定義或不可訪問的屬性上調(diào)用isset()或empty()時(shí),將調(diào)用__isset()方法。下面是一個例子:

          <?php class Person {     public $sex;     private $name;     private $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      /**      * @param $content      *      * @return bool      */     public function __isset($content) {         echo "The {$content} property is private,the __isset() method is called automatically.<br>";         echo  isset($this->$content);     } }  $person = new Person("John", 25); // Initially assigned. echo isset($person->sex),"<br>"; echo isset($person->name),"<br>"; echo isset($person->age),"<br>";

          代碼運(yùn)行結(jié)果如下:

          1 The name property is private,the __isset() method is called automatically. 1 The age property is private,the __isset() method is called automatically. 1

          8. __unset()

          isset()方法類似,當(dāng)在未定義或不可訪問的屬性上調(diào)用unset()方法時(shí),將調(diào)用unset()方法。下面是一個例子:

          <?php class Person {     public $sex;     private $name;     private $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      /**      * @param $content      *      * @return bool      */     public function __unset($content) {         echo "It is called automatically when we use the unset() method outside the class.<br>";         echo  isset($this->$content);     } }  $person = new Person("John", 25); // Initially assigned. unset($person->sex),"<br>"; unset($person->name),"<br>"; unset($person->age),"<br>";

          代碼的運(yùn)行結(jié)果如下:

          It is called automatically when we use the unset() method outside the class. 1 It is called automatically when we use the unset() method outside the class. 1

          9. __sleep()

          serialize()方法將檢查類中是否有魔術(shù)方法__sleep()。如果存在,將首先調(diào)用該方法,然后執(zhí)行序列化操作。

          __sleep()方法通常用于指定保存數(shù)據(jù)之前需要序列化的屬性。如果有一些非常大的對象不需要全部保存,那么您會發(fā)現(xiàn)此功能非常有用。

          有關(guān)詳細(xì)信息,請參考以下代碼:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      /**      * @return array      */     public function __sleep() {         echo "It is called when the serialize() method is called outside the class.<br>";         $this->name = base64_encode($this->name);         return array('name', 'age'); // It must return a value of which the elements are the name of the properties returned.     } }  $person = new Person('John'); // Initially assigned. echo serialize($person); echo '<br/>';

          代碼運(yùn)行結(jié)果如下:

          It is called when the serialize() method is called outside the class. O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}

          10. __wakeup()

          sleep()方法相比,wakeup()方法通常用于反序列化操作,例如重建數(shù)據(jù)庫連接或執(zhí)行其他初始化操作。

          下面是相關(guān)實(shí)例:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      /**      * @return array      */     public function __sleep() {         echo "It is called when the serialize() method is called outside the class.<br>";         $this->name = base64_encode($this->name);         return array('name', 'age'); // It must return a value of which the elements are the name of the properties returned.     }      /**      * __wakeup      */     public function __wakeup() {         echo "It is called when the unserialize() method is called outside the class.<br>";         $this->name = 2;         $this->sex = 'Male';         // There is no need to return an array here.     } }  $person = new Person('John'); // Initially assigned. var_dump(serialize($person)); var_dump(unserialize(serialize($person)));

          代碼運(yùn)行結(jié)果如下:

          It is called when the serialize() method is called outside the class. string(58) "O:6:"Person":2:{s:4:"name";s:8:"5bCP5piO";s:3:"age";i:25;}" It is called when the unserialize() method is called outside the class. object(Person)#2 (3) { ["sex"]=> string(3) "Male" ["name"]=> int(2) ["age"]=> int(25) }

          11. __toString()

          使用echo方法直接打印對象時(shí),將調(diào)用__toString()方法。

          注意:此方法必須返回一個字符串,否則將在E_RECOVERABLE_ERROR級別上引發(fā)致命錯誤。而且您也不能在__toString()方法中拋出異常。

          下面是相關(guān)的實(shí)例:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      public function __toString()     {         return  'go go go';     } }  $person = new Person('John'); // Initially assigned. echo $person;

          運(yùn)行代碼結(jié)果如下:

          go go go

          那么,如果在類中未定義__toString()方法怎么辦?讓我們嘗試一下。

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }  }  $person = new Person('John'); // Initially assigned. echo $person;

          運(yùn)行代碼結(jié)果如下:

          Catchable fatal error: Object of class Person could not be converted to string in D:phpStudyWWWtestindex.php on line 18

          顯然,它在頁面上報(bào)告了一個致命錯誤,PHP語法不支持這樣的寫法。

          12. __invoke()

          當(dāng)您嘗試以調(diào)用函數(shù)的方式調(diào)用對象時(shí),__ invoke()方法將被自動調(diào)用。

          注意:此功能僅在PHP 5.3.0及更高版本中有效。

          下面是相關(guān)實(shí)例:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      public function __invoke() {         echo 'This is an object';     }  }  $person = new Person('John'); // Initially assigned. $person();

          運(yùn)行代碼結(jié)果如下:

          This is an object

          如果堅(jiān)持使用對象作為方法(但未定義__invoke()方法),則將得到以下結(jié)果:

          Fatal error: Function name must be a string in D:phpStudyWWWtestindex.php on line 18

          13.__set_state()

          從PHP 5.1.0開始,在調(diào)用var_export()導(dǎo)出類代碼時(shí)會自動調(diào)用__set_state()方法。

          __set_state()方法的參數(shù)是一個包含所有屬性值的數(shù)組,其格式為array('property'=> value,…)

          在以下示例中,我們沒有定義__set_state()方法:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }  }  $person = new Person('John'); // Initially assigned. var_export($person);

          執(zhí)行代碼結(jié)果如下:

          Person::__set_state(array( 'sex' => 'Male', 'name' => 'John', 'age' => 25, ))

          顯然,對象的屬性已打印。

          現(xiàn)在讓我們看看定義__set_state()方法的另一種情況:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      public static function __set_state($an_array)     {         $a = new Person();         $a->name = $an_array['name'];         return $a;     }  }  $person = new Person('John'); // Initially assigned. $person->name = 'Jams'; var_export($person);

          執(zhí)行代碼結(jié)果如下:

          Person::__set_state(array( 'sex' => 'Male', 'name' => 'Jams', 'age' => 25, ))

          14. __clone()

          在PHP中,我們可以使用clone關(guān)鍵字通過以下語法克隆對象:

          $copy_of_object = clone $object;

          但是,使用clone關(guān)鍵字只是一個淺拷貝,因?yàn)樗幸玫膶傩匀詫⒅赶蛟甲兞俊?/p>

          如果在對象中定義了clone()方法,則將在復(fù)制生成的對象中調(diào)用clone()方法,該方法可用于修改屬性的值(如有必要)。

          下面是相關(guān)的示例:

          <?php class Person {     public $sex;     public $name;     public $age;      public function __construct($name="",  $age=25, $sex='Male')     {         $this->name = $name;         $this->age  = $age;         $this->sex  = $sex;     }      public function __clone()     {         echo __METHOD__."your are cloning the object.<br>";     }  }  $person = new Person('John'); // Initially assigned. $person2 = clone $person;  var_dump('persion1:'); var_dump($person); echo '<br>'; var_dump('persion2:'); var_dump($person2);

          運(yùn)行代碼結(jié)果如下:

          Person::__clone your are cloning the object. string(9) "persion1:" object(Person)#1 (3) { ["sex"]=> string(3) "Male" ["name"]=> string(6) "John" ["age"]=> int(25) } string(9) "persion2:" object(Person)#2 (3) { ["sex"]=> string(3) "Male" ["name"]=> string(6) "John" ["age"]=> int(25) }

          15.__autoload()

          __autoload()方法可以嘗試加載未定義的類。

          過去,如果要在程序文件中創(chuàng)建100個對象,則必須使用include()或require()來包含100個類文件,或者必須在同一類文件中定義100個類。 例如以下:

          /**  * file non_autoload.php  */  require_once('project/class/A.php'); require_once('project/class/B.php'); require_once('project/class/C.php'); . . .  if (ConditionA) {     $a = new A();     $b = new B();     $c = new C();     // … } else if (ConditionB) {     $a = newA();     $b = new B();     // … }

          那么,如果我們使用__autoload()方法呢?

          /**  * file autoload_demo.php  */ function  __autoload($className) {     $filePath = “project/class/{$className}.php”;     if (is_readable($filePath)) {         require($filePath);     } }  if (ConditionA) {     $a = new A();     $b = new B();     $c = new C();     // … } else if (ConditionB) {     $a = newA();     $b = new B();     // … }

          當(dāng)PHP引擎第一次使用類A時(shí),如果未找到類A,則autoload方法將被自動調(diào)用,并且類名稱“ A”將作為參數(shù)傳遞。因此,我們在autoload()方法中需要做的是根據(jù)類名找到相應(yīng)的類文件,然后將其包含在內(nèi)。如果找不到該文件,則php引擎將拋出異常。

          16. __debugInfo()

          當(dāng)執(zhí)行 var_dump() 方法時(shí),__debugInfo() 方法會被自動調(diào)用。如果 __debugInfo() 方法未被定義,那么 var_dump 方法或打印出這個對象的所有屬性。

          舉例說明:

          <?php class C {     private $prop;      public function __construct($val) {         $this->prop = $val;     }      /**      * @return array      */     public function __debugInfo() {         return [             'propSquared' => $this->prop ** 2,         ];     } }  var_dump(new C(42));

          執(zhí)行結(jié)果:

          object(C)#1 (1) { ["propSquared"]=> int(1764) }

          注意:__debugInfo() 方法應(yīng)該在 PHP 5.6.0 及以上版本中使用。

          總結(jié)

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號