Membawa anda melalui 16 kaedah sihir PHP
Apakah kaedah ajaib? Artikel ini akan membawa anda melalui 16 kaedah ajaib yang mesti diketahui dan diketahui oleh pembangun PHP. Saya harap ia akan membantu anda!
Dalam PHP, kaedah dinamakan bermula dengan garis bawah berganda (__) dipanggil kaedah ajaib dalam PHP. Kaedah sihir termasuk:
方法名 | 描述 |
---|---|
__construct() | 类的构造函数 |
__destruct() | 类的析构函数 |
__call($funName, $arguments) | 当调用一个未定义或不可达方法时, __call() 方法将被调用。 |
__callStatic($funName, $arguments) | 当调用一个未定义或不可达的静态方法时, __callStatic() 方法将被调用。 |
__get($propertyName) | 当获取一个类的成员变量时, __get() 方法将被调用。 |
__set($property, $value) | 当赋值一个类的成员变量时, __set() 方法将被调用。 |
__isset($content) | 当调用 isset() 或 empty() 对一个未定义或不可达的成员赋值时, __isset() 方法将被调用。 |
__unset($content) | 当调用 reset() 对一个未定义或不可达的成员更新时, __unset() 方法将被调用。 |
__sleep() | 当执行序列化 serialize() 时,__sleep() 方法将首先被调用。 |
__wakeup() | 当执行反序列化 deserialization() 时, __wakeup() 方法将首先被调用。 |
__toString() | 当使用 echo 方法直接输出显示对象时,__toString() 方法首先被调用。 |
__invoke() | 使用调用函数(function)访问一个对象时, __invoke() 方法将首先被调用。 |
__set_state($an_array) | 当调用 var_export() 方法时,__set_state() 方法将被调用。 |
__clone() | 当对象被复制赋值时,__clone() 方法将被调用。 |
__autoload($className) | 试图载入一个未定义的类时调用。 |
__debugInfo() | 输出 debug 信息。 |
Artikel ini akan menggunakan beberapa contoh untuk menunjukkan penggunaan kaedah sihir PHP.
1.__construct()
Apabila objek dicipta, kaedah pembina kelas PHP ialah kaedah pertama yang dipanggil. Setiap kelas mempunyai kaedah pembina. Jika anda tidak mentakrifkannya secara eksplisit dalam kelas, akan ada pembina kelas tanpa hujah lalai, walaupun ia tidak akan ditakrifkan dalam kelas.
1) Aplikasi kaedah pembina
Kaedah pembina kelas biasanya digunakan untuk melaksanakan beberapa tugas pemula, seperti memulakan dan memberikan nilai kepada ahli semasa mencipta objek.
2) Format pengisytiharan kaedah pembina dalam kelas
function __constrct([parameter list]){ 方法具体实现 //通常为成员变量初始赋值。 }
Nota: Hanya satu pembina boleh diisytiharkan dalam kebanyakan kelas. Kerana, PHP tidak menyokong beban berlebihan pembina.
Berikut ialah contoh lengkap:
<?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; } }
Buat objek $Person1 tanpa parameter.
$Person1 = new Person(); echo $Person1->say(); //显示:Name:,Sex:Male,Age:22
Buat objek $Person2 dengan satu hujah "Jams".
$Person2 = new Person("Jams"); echo $Person2->say(); // 显示: Name: Jams, Sex: Male, Age: 22
Panggil dengan 3 parameter untuk mencipta objek $Person3.
$Person3 = new Person ("Jack", "Male", 25); echo $Person3->say(); // 显示:Name: Jack, Sex: Male, Age: 25
__destruct()
Pemusnah ialah lawan kepada pembina.
Pemusnah membenarkan anda melakukan beberapa operasi sebelum memusnahkan objek, seperti menutup fail, mengosongkan set keputusan, dsb.
Destructor ialah ciri baharu yang diperkenalkan dalam PHP 5.
Pemusnah diisytiharkan serupa dengan pembina, bermula dengan dua garis bawah dan nama ditetapkan kepada __destruct()
.
Pengisytiharan pemusnah
function __destruct() { //method body }
Pemusnah tidak boleh mengambil parameter.
Penggunaan pemusnah
Pemusnah biasanya tidak biasa dalam kelas. Ia adalah bahagian pilihan kelas dan biasanya digunakan untuk menyelesaikan beberapa tugas pembersihan sebelum kelas dimusnahkan.
Ini ialah contoh penggunaan pemusnah:
<?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
Hasil keluaran
Well, my name is John
__call()
Kaedah ini menerima dua parameter . Parameter pertama ialah nama kaedah yang tidak ditentukan, dan parameter kedua ialah tatasusunan parameter yang dihantar ke kaedah
Apabila menggunakan
function __call(string $function_name, array $arguments) { // method body }
untuk memanggil kaedah yang tidak ditentukan dalam atur cara , kaedah __call()
akan dipanggil.
Contoh
<?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();
Paparkan hasil
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()
Apabila kaedah statik yang tidak ditentukan dipanggil dalam atur cara, <.> kaedah akan dipanggil secara automatik. __callStatic()
digunakan sama dengan __callStatic()
. Berikut ialah contoh: __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); // 打印出未定义方法的参数列表。 echo ")does not exist!<br>\n"; } } $Person = new Person(); $Person::run("teacher"); // 如果此项目内不存在的方法被调用了,那么 __callStatic() 方法将被自动调用。 $Person::eat("John", "apple"); $Person->say();
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!
. Kaedah ini boleh mendapatkan nilai harta persendirian dari luar objek. Contohnya, __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
set() dicetuskan dan parameter yang diluluskan ialah nama dan nilai sifat yang ditetapkan.
Berikut ialah kod demo:
Hasil jalankan kod:<?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()方法,程序将报错。 $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
Jika kaedah isset() digunakan di luar objek, terdapat dua situasi:
Jika parameter ialah harta awam, anda boleh menggunakan kaedah isset() untuk menentukan sama ada parameter ditetapkan harta. Kaedah isset() tidak akan berfungsi jika parameter ialah harta peribadi.- Jadi untuk harta persendirian, adakah cara untuk mengetahui sama ada ia ditetapkan? Sudah tentu, selagi kaedah __isset() ditakrifkan dalam kelas, kaedah isset() boleh digunakan di luar kelas untuk menentukan sama ada harta persendirian ditetapkan.
- Kaedah __isset() dipanggil apabila isset() atau empty() dipanggil pada harta yang tidak ditentukan atau tidak boleh diakses. Berikut ialah contoh:
<?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
unset() dipanggil apabila kaedah unset() dipanggil pada sifat yang tidak ditentukan atau tidak boleh diakses. Berikut ialah contoh:
Hasil larian kod adalah seperti berikut:
<?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
__sleep() biasanya digunakan untuk menentukan atribut yang perlu disiri sebelum menyimpan data. Anda mungkin mendapati ciri ini berguna jika anda mempunyai beberapa objek yang sangat besar yang tidak semuanya perlu disimpan.
Untuk butiran, sila rujuk kod berikut:
Keputusan yang dijalankan adalah seperti berikut:<?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/>';
dengan Berbanding dengan kaedah
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;}
wakeup() biasanya digunakan untuk operasi penyahserikatan, seperti mewujudkan semula sambungan pangkalan data atau melakukan operasi pemulaan lain.
Berikut ialah contoh yang berkaitan:
Keputusan yang dijalankan adalah seperti berikut:<?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) }
Nota: Kaedah ini mesti mengembalikan rentetan, jika tidak ralat maut akan dinaikkan pada tahap
. Dan anda tidak boleh membuang pengecualian dalam kaedah __toString() sama ada. Berikut ialah contoh yang berkaitan:E_RECOVERABLE_ERROR
<?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
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()
当您尝试以调用函数的方式调用对象时,__ invoke()方法将被自动调用。
注意:此功能仅在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开始,在调用var_export()导出类代码时会自动调用__set_state()方法。
__set_state()方法的参数是一个包含所有属性值的数组,其格式为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);
执行代码结果如下:
Person::__set_state(array( 'sex' => 'Male', 'name' => 'John', 'age' => 25, ))
显然,对象的属性已打印。
现在让我们看看定义__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);
执行代码结果如下:
Person::__set_state(array( 'sex' => 'Male', 'name' => 'Jams', 'age' => 25, ))
14. __clone()
在PHP中,我们可以使用clone关键字通过以下语法克隆对象:
$copy_of_object = clone $object;
但是,使用clone关键字只是一个浅拷贝,因为所有引用的属性仍将指向原始变量。
如果在对象中定义了clone()方法,则将在复制生成的对象中调用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()方法可以尝试加载未定义的类。
过去,如果要在程序文件中创建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方法将被自动调用,并且类名称“ A”将作为参数传递。因此,我们在autoload()方法中需要做的是根据类名找到相应的类文件,然后将其包含在内。如果找不到该文件,则php引擎将抛出异常。
16. __debugInfo()
当执行 var_dump()
方法时,__debugInfo()
方法会被自动调用。如果 __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));
执行结果:
object(C)#1 (1) { ["propSquared"]=> int(1764) }
注意:__debugInfo() 方法应该在 PHP 5.6.0 及以上版本中使用。
总结
以上就是我所了解的 PHP
魔术方法,其中常用的包括 __set()
还有 __get()
和 __autoload()
。如果你还有其他疑问,可以从 PHP
官方网站获得更多帮助。
原文地址:https://www.tutorialdocs.com/article/16-php-magic-methods.html
译文地址:https://learnku.com/php/t/40919
推荐学习:《PHP视频教程》
Atas ialah kandungan terperinci Membawa anda melalui 16 kaedah sihir PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

Jika anda seorang pembangun PHP yang berpengalaman, anda mungkin merasakan bahawa anda telah berada di sana dan telah melakukannya. Anda telah membangunkan sejumlah besar aplikasi, menyahpenyahpepijat berjuta-juta baris kod dan mengubah suai sekumpulan skrip untuk mencapai op

Tutorial ini menunjukkan cara memproses dokumen XML dengan cekap menggunakan PHP. XML (bahasa markup extensible) adalah bahasa markup berasaskan teks yang serba boleh yang direka untuk pembacaan manusia dan parsing mesin. Ia biasanya digunakan untuk penyimpanan data

JWT adalah standard terbuka berdasarkan JSON, yang digunakan untuk menghantar maklumat secara selamat antara pihak, terutamanya untuk pengesahan identiti dan pertukaran maklumat. 1. JWT terdiri daripada tiga bahagian: header, muatan dan tandatangan. 2. Prinsip kerja JWT termasuk tiga langkah: menjana JWT, mengesahkan JWT dan muatan parsing. 3. Apabila menggunakan JWT untuk pengesahan di PHP, JWT boleh dijana dan disahkan, dan peranan pengguna dan maklumat kebenaran boleh dimasukkan dalam penggunaan lanjutan. 4. Kesilapan umum termasuk kegagalan pengesahan tandatangan, tamat tempoh, dan muatan besar. Kemahiran penyahpepijatan termasuk menggunakan alat debugging dan pembalakan. 5. Pengoptimuman prestasi dan amalan terbaik termasuk menggunakan algoritma tandatangan yang sesuai, menetapkan tempoh kesahihan dengan munasabah,

Rentetan adalah urutan aksara, termasuk huruf, nombor, dan simbol. Tutorial ini akan mempelajari cara mengira bilangan vokal dalam rentetan yang diberikan dalam PHP menggunakan kaedah yang berbeza. Vokal dalam bahasa Inggeris adalah a, e, i, o, u, dan mereka boleh menjadi huruf besar atau huruf kecil. Apa itu vokal? Vokal adalah watak abjad yang mewakili sebutan tertentu. Terdapat lima vokal dalam bahasa Inggeris, termasuk huruf besar dan huruf kecil: a, e, i, o, u Contoh 1 Input: String = "TutorialSpoint" Output: 6 menjelaskan Vokal dalam rentetan "TutorialSpoint" adalah u, o, i, a, o, i. Terdapat 6 yuan sebanyak 6

Mengikat statik (statik: :) Melaksanakan pengikatan statik lewat (LSB) dalam PHP, yang membolehkan kelas panggilan dirujuk dalam konteks statik dan bukannya menentukan kelas. 1) Proses parsing dilakukan pada masa runtime, 2) Cari kelas panggilan dalam hubungan warisan, 3) ia boleh membawa overhead prestasi.

Apakah kaedah sihir PHP? Kaedah sihir PHP termasuk: 1. \ _ \ _ Membina, digunakan untuk memulakan objek; 2. \ _ \ _ Destruct, digunakan untuk membersihkan sumber; 3. \ _ \ _ Call, mengendalikan panggilan kaedah yang tidak wujud; 4. \ _ \ _ Mendapatkan, melaksanakan akses atribut dinamik; 5. \ _ \ _ Set, melaksanakan tetapan atribut dinamik. Kaedah ini secara automatik dipanggil dalam situasi tertentu, meningkatkan fleksibiliti dan kecekapan kod.
