Cangkuk Harta PHP
pengenalan
PHP 8.4 akan dikeluarkan pada November 2024 dan akan membawakan ciri baharu yang menarik: pengait harta benda.
Dalam artikel ini, kami akan melihat apakah cangkuk hartanah dan cara anda boleh menggunakannya dalam projek PHP 8.4 anda.
Sebagai nota sampingan, anda mungkin juga berminat untuk menyemak artikel saya yang lain yang menunjukkan kepada anda fungsi tatasusunan baharu yang ditambah dalam PHP 8.4.
Apakah itu PHP Property Hooks?
Cakuk harta membolehkan anda menentukan logik pengambil dan penetap tersuai untuk sifat kelas tanpa perlu menulis kaedah pengambil dan penetap yang berasingan. Ini bermakna anda boleh mentakrifkan logik secara langsung dalam pengisytiharan harta supaya anda boleh mengakses terus harta (seperti $user->firstName) tanpa perlu ingat untuk memanggil kaedah (seperti $user->getFirstName() dan $user- >setFirstName()).
Anda boleh menyemak RFC untuk ciri ini di https://wiki.php.net/rfc/property-hooks
Jika anda seorang pembangun Laravel, semasa anda membaca artikel ini, anda mungkin perasan bahawa cangkuk kelihatan sangat serupa dengan pengakses dan mutator dalam model Laravel.
Saya sangat menyukai rupa ciri cangkuk hartanah dan saya membayangkan ia adalah sesuatu yang saya akan gunakan dalam projek saya apabila PHP 8.4 dikeluarkan.
Untuk memahami cara cangkuk hartanah berfungsi, mari kita lihat beberapa contoh penggunaan.
Cangkuk "dapatkan".
Anda boleh menentukan cangkuk get yang akan dipanggil apabila anda cuba mengakses harta benda.
Sebagai contoh, bayangkan anda mempunyai kelas Pengguna ringkas yang menerima nama pertama dan Nama akhir dalam pembina. Anda mungkin mahu mentakrifkan sifat Nama penuh yang menggabungkan nama pertama dan nama akhir bersama-sama. Untuk melakukan ini, anda boleh menentukan cangkuk get untuk sifat nama penuh:
readonly class User { public string $fullName { get { return $this->firstName.' '.$this->lastName; } } public function __construct( public readonly string $firstName, public readonly string $lastName ) { // } } $user = new User(firstName: 'ash', lastName: 'allen'); echo $user->firstName; // ash echo $user->lastName; // allen echo $user->fullName; // ash allen
Dalam contoh di atas, kita dapat melihat bahawa kita telah menentukan get hook untuk sifat nama penuh yang mengembalikan nilai yang dikira dengan menggabungkan sifat firstName dan lastName bersama-sama. Kita boleh membersihkannya sedikit lagi dengan menggunakan sintaks yang serupa dengan fungsi anak panah juga:
readonly class User { public string $fullName { get => $this->firstName.' '.$this->lastName; } public function __construct( public readonly string $firstName, public readonly string $lastName, ) { // } } $user = new User(firstName: 'ash', lastName: 'allen'); echo $user->firstName; // ash echo $user->lastName; // allen echo $user->fullName; // ash allen
Jenis Keserasian
Perlu ambil perhatian bahawa nilai yang dikembalikan daripada pengambil mesti serasi dengan jenis harta benda.
Jika jenis yang ketat tidak didayakan, nilai akan ditaip-dijuggle ke jenis sifat. Sebagai contoh, jika anda mengembalikan integer daripada sifat yang diisytiharkan sebagai rentetan, integer akan ditukar kepada rentetan:
declare(strict_types=1); class User { public string $fullName { get { return 123; } } public function __construct( public readonly string $firstName, public readonly string $lastName, ) { // } } $user = new User(firstName: 'ash', lastName: 'allen'); echo $user->fullName; // "123"
Dalam contoh di atas, walaupun kami telah menentukan 123 sebagai integer untuk dikembalikan, "123" dikembalikan sebagai rentetan kerana sifat itu ialah rentetan.
Kami boleh menambah declare(strict_types=1); ke bahagian atas kod seperti itu untuk membolehkan semakan jenis yang ketat:
declare(strict_types=1); class User { public string $fullName { get { return 123; } } public function __construct( public readonly string $firstName, public readonly string $lastName, ) { // } }
Sekarang ini akan menyebabkan ralat dilemparkan kerana nilai pulangan ialah integer, tetapi sifatnya ialah rentetan:
Fatal error: Uncaught TypeError: User::$fullName::get(): Return value must be of type string, int returned
Cangkuk "set".
Cangkok harta PHP 8.4 juga membolehkan anda menentukan cangkuk set. Ini dipanggil apabila anda cuba menetapkan sifat.
Anda boleh memilih antara dua sintaks berasingan untuk cangkuk set:
- Mentakrifkan secara eksplisit nilai untuk ditetapkan pada harta itu
- Menggunakan fungsi anak panah untuk mengembalikan nilai untuk ditetapkan pada harta
Mari kita lihat kedua-dua pendekatan ini. Kami akan bayangkan kami mahu huruf besar huruf pertama nama pertama dan nama keluarga apabila ia ditetapkan pada kelas Pengguna:
declare(strict_types=1); class User { public string $firstName { // Explicitly set the property value set(string $name) { $this->firstName = ucfirst($name); } } public string $lastName { // Use an arrow function and return the value // you want to set on the property set(string $name) => ucfirst($name); } public function __construct( string $firstName, string $lastName ) { $this->firstName = $firstName; $this->lastName = $lastName; } } $user = new User(firstName: 'ash', lastName: 'allen'); echo $user->firstName; // Ash echo $user->lastName; // Allen
Seperti yang dapat kita lihat dalam contoh di atas, kami telah mentakrifkan cangkuk set untuk sifat firstName yang menggunakan huruf besar pada huruf pertama nama sebelum menetapkannya pada harta tersebut. Kami juga telah menentukan cangkuk set untuk sifat lastName yang menggunakan fungsi anak panah untuk mengembalikan nilai untuk ditetapkan pada harta tersebut.
Jenis Keserasian
Jika harta mempunyai pengisytiharan jenis, maka cangkuk setnya mesti mempunyai set jenis yang serasi juga. Contoh berikut akan mengembalikan ralat kerana cangkuk set untuk firstName tidak mempunyai pengisytiharan jenis, tetapi harta itu sendiri mempunyai pengisytiharan jenis rentetan:
class User { public string $firstName { set($name) => ucfirst($name); } public string $lastName { set(string $name) => ucfirst($name); } public function __construct( string $firstName, string $lastName ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
Percubaan untuk menjalankan kod di atas akan menyebabkan ralat berikut dilemparkan:
Fatal error: Type of parameter $name of hook User::$firstName::set must be compatible with property type
Menggunakan "get" dan "set" Hooks Together
Anda tidak terhad kepada menggunakan cangkuk get dan set secara berasingan. Anda boleh menggunakannya bersama-sama dalam harta yang sama.
Mari kita ambil contoh mudah. Kami akan bayangkan kami mempunyai sifat Nama penuh pada kelas Pengguna kami. Apabila kami menetapkan harta itu, kami akan membahagikan nama penuh kepada nama pertama dan nama keluarga. Saya tahu ini adalah pendekatan yang naif dan terdapat banyak penyelesaian yang lebih baik, tetapi ia semata-mata untuk contoh untuk menyerlahkan sifat ketagih.
Kod mungkin kelihatan seperti itu:
declare(strict_types=1); class User { public string $fullName { // Dynamically build up the full name from // the first and last name get => $this->firstName.' '.$this->lastName; // Split the full name into first and last name and // then set them on their respective properties set(string $name) { $splitName = explode(' ', $name); $this->firstName = $splitName[0]; $this->lastName = $splitName[1]; } } public string $firstName { set(string $name) => $this->firstName = ucfirst($name); } public string $lastName { set(string $name) => $this->lastName = ucfirst($name); } public function __construct(string $fullName) { $this->fullName = $fullName; } } $user = new User(fullName: 'ash allen'); echo $user->firstName; // Ash echo $user->lastName; // Allen echo $user->fullName; // Ash Allen
In the code above, we've defined a fullName property that has both a get and set hook. The get hook returns the full name by concatenating the first and last name together. The set hook splits the full name into the first and last name and sets them on their respective properties.
You may have also noticed that we're not setting a value on the fullName property itself. Instead, if we need to read the value of the fullName property, the get hook will be called to build up the full name from the first and last name properties. I've done this to highlight that you can have a property that doesn't have a value set directly on it, but instead, the value is calculated from other properties.
Using Property Hooks on Promoted Properties
A cool feature of property hooks is that you can also use them with constructor promoted properties.
Let's check out an example of a class that isn't using promoted properties and then look at what it might look like using promoted properties.
Our User class might look like so:
readonly class User { public string $fullName { get => $this->firstName.' '.$this->lastName; } public string $firstName { set(string $name) => ucfirst($name); } public string $lastName { set(string $name) => ucfirst($name); } public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
We could promote the firstName and lastName properties into the constructor and define their set logic directly on the property:
readonly class User { public string $fullName { get => $this->firstName.' '.$this->lastName; } public function __construct( public string $firstName { set (string $name) => ucfirst($name); }, public string $lastName { set (string $name) => ucfirst($name); } ) { // } }
Write-only Hooked Properties
If you define a hooked property with a setter that doesn't actually set a value on the property, then the property will be write-only. This means you can't read the value of the property, you can only set it.
Let's take our User class from the previous example and modify the fullName property to be write-only by removing the get hook:
declare(strict_types=1); class User { public string $fullName { // Define a setter that doesn't set a value // on the "fullName" property. This will // make it a write-only property. set(string $name) { $splitName = explode(' ', $name); $this->firstName = $splitName[0]; $this->lastName = $splitName[1]; } } public string $firstName { set(string $name) => $this->firstName = ucfirst($name); } public string $lastName { set(string $name) => $this->lastName = ucfirst($name); } public function __construct( string $fullName, ) { $this->fullName = $fullName; } } $user = new User('ash allen'); echo $user->fullName; // Will trigger an error!
If we were to run the code above, we'd see the following error being thrown when attempting to access the fullName property:
Fatal error: Uncaught Error: Property User::$fullName is write-only
Read-only Hooked Properties
Similarly, a property can be read-only.
For example, imagine we only ever want the fullName property to be generated from the firstName and lastName properties. We don't want to allow the fullName property to be set directly. We can achieve this by removing the set hook from the fullName property:
class User { public string $fullName { get { return $this->firstName.' '.$this->lastName; } } public function __construct( public readonly string $firstName, public readonly string $lastName, ) { $this->fullName = 'Invalid'; // Will trigger an error! } }
If we were to try and run the code above, the following error would be thrown because we're trying to set the fullName property directly:
Uncaught Error: Property User::$fullName is read-only
Using the "readonly" keyword
You can still make our PHP classes readonly even if they have hooked properties. For example, we may want to make the User class readonly:
readonly class User { public string $firstName { set(string $name) => ucfirst($name); } public string $lastName { set(string $name) => ucfirst($name); } public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
However, a hooked property cannot use the readonly keyword directly. For example, this class would be invalid:
class User { public readonly string $fullName { get => $this->firstName.' '.$this->lastName; } public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
The above code would throw the following error:
Fatal error: Hooked properties cannot be readonly
The "PROPERTY" Magic Constant
In PHP 8.4, a new magic constant called __PROPERTY__ has been introduced. This constant can be used to reference the property name within the property hook.
Let's take a look at an example:
class User { // ... public string $lastName { set(string $name) { echo __PROPERTY__; // lastName $this->{__PROPERTY__} = ucfirst($name); // Will trigger an error! } } public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
In the code above, we can see that using __PROPERTY__ inside the lastName property's setter will output the property name lastName. However, it's also worth noting that trying to use this constant in an attempt to set the property value will trigger an error:
Fatal error: Uncaught Error: Must not write to virtual property User::$lastName
There's a handy use case example for the __PROPERTY__ magic constant that you can check out on GitHub: https://github.com/Crell/php-rfcs/blob/master/property-hooks/examples.md.
Hooked Properties in Interfaces
PHP 8.4 also allows you to define publicly accessible hooked properties in interfaces. This can be useful if you want to enforce that a class implements certain properties with hooks.
Let's take a look at an example interface with hooked properties declared:
interface Nameable { // Expects a public gettable 'fullName' property public string $fullName { get; } // Expects a public gettable 'firstName' property public string $firstName { get; } // Expects a public settable 'lastName' property public string $lastName { set; } }
In the interface above, we're defining that any classes implementing the Nameable interface must have:
- A fullName property that is at least publicly gettable. This can be achieved by defining a get hook or not defining a hook at all.
- A firstName property that is at least publicly gettable.
- A lastName property that is at least publicly settable. This can be achieved by defining a property which has a set hook or not defining a hook at all. But if the class is read-only then the property must have a set hook.
This class that implements the Nameable interface would be valid:
class User implements Nameable { public string $fullName { get => $this->firstName.' '.$this->lastName; } public string $firstName { set(string $name) => ucfirst($name); } public string $lastName; public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
The class above would be valid because the fullName property has a get hook to match the interface definition. The firstName property only has a set hook, but is still publicly accessible so it satisfies the criteria. The lastName property doesn't have a get hook, but it is publicly settable so it satisfies the criteria.
Let's update our User class to enforce a get and set hook for the fullName property:
interface Nameable { public string $fullName { get; set; } public string $firstName { get; } public string $lastName { set; } }
Our User class would no longer satisfy the criteria for the fullName property because it doesn't have a set hook defined. It would cause the following error to be thrown:
Fatal error: Class User contains 1 abstract methods and must therefore be declared abstract or implement the remaining methods (Nameable::$fullName::set)
Hooked Properties in Abstract Classes
Similar to interfaces, you can also define hooked properties in abstract classes. This can be useful if you want to provide a base class that defines hooked properties that child classes must implement. You can also define the hooks in the abstract class and have them be overridden in the child classes.
For example, let's make a Model abstract class that defines a name property that must be implemented by child classes:
abstract class Model { abstract public string $fullName { get => $this->firstName.' '.$this->lastName; set; } abstract public string $firstName { get; } abstract public string $lastName { set; } }
In the abstract class above, we're defining that any classes that extend the Model class must have:
- A fullName property that is at least publicly gettable and settable. This can be achieved by defining a get and set hook or not defining a hook at all. We've also defined the get hook for the fullName property in the abstract class so we don't need to define it in the child classes, but it can be overridden if needed.
- A firstName property that is at least publicly gettable. This can be achieved by defining a get hook or not defining a hook at all.
- A lastName property that is at least publicly settable. This can be achieved by defining a property which has a set hook or not defining a hook at all. But if the class is read-only then the property must have a set hook.
We could then create a User class that extends the Model class:
class User extends Model { public string $fullName; public string $firstName { set(string $name) => ucfirst($name); } public string $lastName; public function __construct( string $firstName, string $lastName, ) { $this->firstName = $firstName; $this->lastName = $lastName; } }
Conclusion
Hopefully, this article should have given you an insight into how PHP 8.4 property hooks work and how you might be able to use them in your PHP projects.
I wouldn't worry too much if this feature seems a little confusing at first. When I first saw it, I was a little confused too (especially with how they work with interfaces and abstract classes). But once you start tinkering with them, you'll soon get the hang of it.
I'm excited to see how this feature will be used in the wild and I'm looking forward to using it in my projects when PHP 8.4 is released.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.
Keep on building awesome stuff! ?
Atas ialah kandungan terperinci Cangkuk Harta 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

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

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

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,

Fungsi penghitungan dalam Php8.1 meningkatkan kejelasan dan jenis keselamatan kod dengan menentukan pemalar yang dinamakan. 1) Penghitungan boleh menjadi bilangan bulat, rentetan atau objek, meningkatkan kebolehbacaan kod dan keselamatan jenis. 2) Penghitungan adalah berdasarkan kelas dan menyokong ciri-ciri berorientasikan objek seperti traversal dan refleksi. 3) Penghitungan boleh digunakan untuk perbandingan dan tugasan untuk memastikan keselamatan jenis. 4) Penghitungan menyokong penambahan kaedah untuk melaksanakan logik kompleks. 5) Pemeriksaan jenis dan pengendalian ralat yang ketat boleh mengelakkan kesilapan biasa. 6) Penghitungan mengurangkan nilai sihir dan meningkatkan keupayaan, tetapi memberi perhatian kepada pengoptimuman prestasi.

Sesi rampasan boleh dicapai melalui langkah -langkah berikut: 1. Dapatkan ID Sesi, 2. Gunakan ID Sesi, 3. Simpan sesi aktif. Kaedah untuk mengelakkan rampasan sesi dalam PHP termasuk: 1. Gunakan fungsi Sesi_Regenerate_ID () untuk menjana semula ID Sesi, 2. Data sesi stor melalui pangkalan data, 3.

Penerapan prinsip pepejal dalam pembangunan PHP termasuk: 1. Prinsip Tanggungjawab Tunggal (SRP): Setiap kelas bertanggungjawab untuk hanya satu fungsi. 2. Prinsip Terbuka dan Tutup (OCP): Perubahan dicapai melalui lanjutan dan bukannya pengubahsuaian. 3. Prinsip Penggantian Lisch (LSP): Subkelas boleh menggantikan kelas asas tanpa menjejaskan ketepatan program. 4. Prinsip Pengasingan Antara Muka (ISP): Gunakan antara muka halus untuk mengelakkan kebergantungan dan kaedah yang tidak digunakan. 5. Prinsip Inversi Ketergantungan (DIP): Modul peringkat tinggi dan rendah bergantung kepada abstraksi dan dilaksanakan melalui suntikan ketergantungan.

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.

Prinsip reka bentuk Restapi termasuk definisi sumber, reka bentuk URI, penggunaan kaedah HTTP, penggunaan kod status, kawalan versi, dan benci. 1. Sumber harus diwakili oleh kata nama dan dikekalkan pada hierarki. 2. Kaedah HTTP harus mematuhi semantik mereka, seperti GET digunakan untuk mendapatkan sumber. 3. Kod status hendaklah digunakan dengan betul, seperti 404 bermakna sumber tidak wujud. 4. Kawalan versi boleh dilaksanakan melalui URI atau header. 5. Boots Operasi Pelanggan Hateoas melalui pautan sebagai tindak balas.

Dalam PHP, pengendalian pengecualian dicapai melalui percubaan, menangkap, akhirnya, dan membuang kata kunci. 1) blok percubaan mengelilingi kod yang boleh membuang pengecualian; 2) Blok tangkapan mengendalikan pengecualian; 3) Akhirnya Blok memastikan bahawa kod itu sentiasa dilaksanakan; 4) Lemparan digunakan untuk membuang pengecualian secara manual. Mekanisme ini membantu meningkatkan keteguhan dan mengekalkan kod anda.

Fungsi utama kelas tanpa nama dalam PHP adalah untuk membuat objek satu kali. 1. Kelas tanpa nama membenarkan kelas tanpa nama ditakrifkan secara langsung dalam kod, yang sesuai untuk keperluan sementara. 2. Mereka boleh mewarisi kelas atau melaksanakan antara muka untuk meningkatkan fleksibiliti. 3. Beri perhatian kepada prestasi dan kebolehbacaan kod apabila menggunakannya, dan elakkan berulang kali menentukan kelas tanpa nama yang sama.
