溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php魔術方法有哪些及怎么使用

發(fā)布時間:2023-01-05 09:35:52 來源:億速云 閱讀:92 作者:iii 欄目:編程語言

這篇文章主要介紹“php魔術方法有哪些及怎么使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“php魔術方法有哪些及怎么使用”文章能幫助大家解決問題。

PHP中以雙下劃線“__”開始命名的內(nèi)置方法被稱作魔術方法,包括“__construct()”、“__set()”、“__get()”、“__isset()”、“__unset()”、“__sleep()”、“__wakeup()”、“__call()”、“__invoke()”等;其中“__construct()”是類的構造方法,是對象創(chuàng)建完成后第一個被對象自動調(diào)用的方法。

php中的魔術方法

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

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

本文將使用一些實例展示 PHP 魔術方法的運用。

1.__construct()

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

1) 構造方法的運用

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

2) 類中構造方法的聲明格式

function __constrct([parameter list]){

    方法具體實現(xiàn) //通常為成員變量初始賦值。

}

注意: 在多數(shù)類中僅可以聲明一個構造方法。因為, PHP 不支持構造方法重載。

下面是個完整的例子:

<?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 方法定義
         */
        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()

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

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

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

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

析構函數(shù)的聲明

function __destruct()
{
    //method body
}

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

析構函數(shù)的使用

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

這是使用析構函數(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

輸出結果

Well, my name is John

__call()

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

使用

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

在程序中調(diào)用未定義方法時, __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();

顯示結果

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()

當在程序中調(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"); // 如果此項目內(nèi)不存在的方法被調(diào)用了,那么 __callStatic() 方法將被自動調(diào)用。
$Person::eat("John", "apple");
$Person->say();

執(zhí)行結果如下:

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()

當你嘗試在外部訪問對象的私有屬性時,應用程序?qū)伋霎惓2⒔Y束運行。我們可以使用 __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.

結果顯示如下

Name: John
Age: 50

6. __set()

set($property,$value)方法用于設置類的私有屬性。分配了未定義的屬性后,將觸發(fā)set()方法,并且傳遞的參數(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ū)箦e。
$Person->age = 16; // "age"屬性修改成功。
$Person->age = 160; //160是無效值,因此修改失敗。
$Person->say();  //輸出:My name is Lili, I'm 16 years old。

代碼運行結果:

My name is Lili, I'm 16 years old

7. __isset()

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

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

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

  2. 如果參數(shù)是私有屬性,則isset()方法將不起作用。

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

當在未定義或不可訪問的屬性上調(diào)用isset()或empty()時,將調(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>";

代碼運行結果如下:

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()方法類似,當在未定義或不可訪問的屬性上調(diào)用unset()方法時,將調(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>";

代碼的運行結果如下:

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()方法將檢查類中是否有魔術方法__sleep()。如果存在,將首先調(diào)用該方法,然后執(zhí)行序列化操作。

__sleep()方法通常用于指定保存數(shù)據(jù)之前需要序列化的屬性。如果有一些非常大的對象不需要全部保存,那么您會發(fā)現(xià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;
    }

    /**
     * @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/>';

代碼運行結果如下:

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í)行其他初始化操作。

下面是相關實例:

<?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)));

代碼運行結果如下:

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方法直接打印對象時,將調(diào)用__toString()方法。

注意:此方法必須返回一個字符串,否則將在E_RECOVERABLE_ERROR級別上引發(fā)致命錯誤。而且您也不能在__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;
    }

    public function __toString()
    {
        return  'go go go';
    }
}

$person = new Person('John'); // Initially assigned.
echo $person;

運行代碼結果如下:

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;

運行代碼結果如下:

Catchable fatal error: Object of class Person could not be converted to string in D:\phpStudy\WWW\test\index.php on line 18

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

12. __invoke()

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

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

下面是相關實例:

<?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();

運行代碼結果如下:

This is an object

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

Fatal error: Function name must be a string in D:\phpStudy\WWW\test\index.php on line 18

13.__set_state()

從PHP 5.1.0開始,在調(diào)用var_export()導出類代碼時會自動調(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í)行代碼結果如下:

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í)行代碼結果如下:

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

14. __clone()

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

$copy_of_object = clone $object;

但是,使用clone關鍵字只是一個淺拷貝,因為所有引用的屬性仍將指向原始變量。

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

下面是相關的示例:

<?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);

運行代碼結果如下:

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();
    // …
}

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

16. __debugInfo()

當執(zhí)行 var_dump() 方法時,__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í)行結果:

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

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

關于“php魔術方法有哪些及怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

php
AI