Introducing the return type declaration of PHP 7

Last week I wrote about type ting in PHP. Type hints further clarify the intent of the function and make the value display a value of a specific type. If you use a function that only allows values of a specific type, you do not need to check the variable type in the function, but this does not mean that you do not need to check the type before calling the function. If an incorrect variable type is passed, a fatal error occurs. For example, get\u post\u meta() can return almost all types of data. If you use the expected return array method, you must also ensure that the expected result is returned. Therefore, if a function needs to sort one of its parameters, it must ensure that the expected type of result is obtained in get_post\u meta(), not false or other types. The
If you can build abstractions around get\u post\u meta() that guarantees that arrays are always provided, you can safely pass the return value to the function that needs the array. PHP 5 allows you to write functions that must always return arrays, but cannot declare arrays programmatically. Yes, inline documentation can help you understand what humans and ides should expect. But it does not help the compiler or guarantee results. PHP 7 introduces a return type declaration, which allows you to do this. The return type declaration makes the return value of a function must be of a specific type. The
There are two main parameters for the pros and cons of using return type declarations return type declarations. The first is the performance that is directly affected because the return type is explicitly declared. This means that the PHP compiler doesn’t need to know about it, so it compiles faster. Other parameters declared with return types are similar to using type hints. Clarify the intent and intended use of the code. As with type hints, languages that dynamically specify types lose some flexibility. Therefore, you must decide whether flexibility is worth trading for greater clarity and possible performance. The
In addition, since the semi ring watermark is not compatible with PHP 5, it is not a good idea to use it in the word press plug-in or theme that will be launched soon. WordPress is built even in version checking. PHP. Org does not allow plug-ins or themes to use PHP 5.5 or later syntax. Conversely, if you are working on a custom site running PHP 7, and these PHP 7 features are useful, why not use them? Yes, in the introduction of this post, I discussed the problem of relying on the assumption that functions such as get_post\u meta() always return the expected value. What happens if you always put an array in a specific meta key, but do not set the data of a specific post, or try to get the post metadata of a non-existent post? The
If the goal is to use the post meta value in a function that requires an array, PHP 5 must test the value returned each time get_post\u meta(). In PHP 7, you can create abstractions around get_post\u meta(). This abstraction ensures that an array is always returned and is explicitly created through a return type declaration. The following is an example of a class that performs this operation: Male post = $post;
}The
Public function get_all(): array{
Return get_post_meta ($this->post->id);
}The
Public function get\u key ($meta\u key){
$meta = get\u post\u meta ($this->post->id, $meta\u key);
If (! Is_array ($meta) & &! Empty ($meta)){
Return [$meta];
}Elseif (empty ($meta){
Returns
}Another{
Return $meta;
}The
}The
}The
Male as you can see
There are two ways to always return an array. This is obvious to the code reader if we know the new syntax. You can also create less code without other type checking code, and the PHP compiler parses faster. The new syntax type prompts for a new syntax that is not compatible with PHP 5. The following is a default example of this syntax: Male posts);
}In PHP 5, this generates the following messages and fatal errors: The
Parsing error: syntax error, unexpected ‘:’, In ‘{‘ expected PHP 7, this is interpreted as a return type declaration. Since the declaration is’ Int ‘, the function must return an integer, otherwise an error will occur. The following is an example of creating a simple word press rest API client using type hints and return type declarations. ‹ root=$wp\u root;
}The
Query public function (int$page){
$headers=array (\
$request= request:: get ($this->root. \
$this->posts = json\u decode ($request->body);
}The
Public function get\u total \u posts(): int{
If (empty ($this->posts) |! Is_array ($this->posts){
Return 0
}The
Return times ($this->posts);
}The
Public function get_posts(): array{
Return $this->posts.
}The
}The last two methods of this class use the return type declaration. The get\u total\u count() method uses the int type declaration, so it must return an integer, while the get\u posts() method uses the array return type declaration, so it must return an array. The
Note that this example uses the requests library included in WordPress 4.6 or later. Do you need to use a return type declaration? PHP is very flexible and easy to learn, so it is very popular. This is largely because dynamic typing provides flexibility. But this flexibility limits PHP’s performance. PHP is word press. COM, Facebook, Wikipedia, Etsy and other websites with the largest traffic on the Internet. The new language rules make PHP less flexible, and it is necessary to provide higher performance to solve the problem of using PHP. The
Personally, I didn’t create the top 10 websites, but this new language configuration is still very important to me. Word press can be run on a small server, which is great. The higher the performance of the code, the less server resources are required, which is a big problem in the WordPress world because the code will run in shared hosting and enterprise environments. Our language is maturing. What we can do with it is very interesting. Hopefully, this article will enable your computer to launch the WordPress web site based on PHP 7 and start learning about these interesting new features. It’s not difficult, but if you want to improve the performance of your site and prepare for WordPress, you must finally switch to a feature that is incompatible with earlier versions of PHP 7 that learned the return type declaration of PHP 7.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *