In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "PHP method parameter type constraint example explanation", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "PHP method parameter type constraint example explanation" bar!
On the method Parameter Type constraint of PHP
After PHP5, PHP formally introduced method parameter type constraints. That is, if the type of method parameter is specified, passing different types of parameters will result in an error. In the PHP manual, the type constraints of methods are limited to classes, interfaces, arrays, or callable callback functions. If you specify a default value of NULL, we can also pass NULL as a parameter.
Class A {}
Function testA (A $a) {
Var_dump ($a)
}
TestA (new A ())
/ / testA (1)
/ / Fatal error: Uncaught TypeError: Argument 1 passed to testA () must be an instance of A, int given
In this example, we define the parameter type as class A, so when we pass a scalar type, we directly return an error message.
Function testB (int $a) {
Var_dump ($a)
}
TestB (1)
TestB ('52aadfdf'); / / the string has been strongly converted to int
/ / testB ('a')
/ / Fatal error: Uncaught TypeError: Argument 1 passed to testB () must be of the type int, string given
Function testC (string $a) {
Var_dump ($a)
}
TestC ('test')
TestC (1); / / the number will be strongly converted into a string
/ / testC (new A ())
/ / Fatal error: Uncaught TypeError: Argument 1 passed to testC () must be of the type string
It is clearly stated in the manual that scalar types cannot use type constraints. But in fact, it can be used, but if they are all scalar types, they will be cast to each other, which will not be a good constraint. For example, in the above example, the int and string types are cast to each other. An error is reported if a non-scalar type is specified. Here is the focus of this article, friends need to draw a line. In fact, to put it bluntly, if we want to specify the type of parameter as a fixed scalar type, it is not a good choice to specify it in the parameter, it is better to determine the type again in the method. And if there is a strong turn in the parameters, it will also lead to a deviation in the internal judgment of the method.
Finally, let's take a look at the type constraints of interfaces and anonymous methods. Anonymous parameter types are common in frameworks such as Laravel.
/ / Interface type
Interface D {}
Class childD implements D {}
Function testD (D $d) {
Var_dump ($d)
}
TestD (new childD ())
/ / callback anonymous function type
Function testE (Callable $e, string $data) {
$e ($data)
}
TestE (function ($data) {
Var_dump ($data)
}, 'callback function')
Thank you for your reading, the above is the content of "PHP method parameter type constraint example explanation". After the study of this article, I believe you have a deeper understanding of PHP method parameter type constraint example explanation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.