PHP type constraint usage example _php tips

Source: Internet
Author: User
PHP is a weak type, which is characterized by the need not to specify a type for the variable, but also to store any type thereafter, but in PHP's new syntax, in some specific cases, for certain types, syntax constraints are also available.

Specific occasions: form parametric of functions (methods)

Specific types: Object types (class names), interface types (interface names), array types (arrays), function types (callable)

function f (class name $p) {}//requires that the argument can only use Object function f (interface name $p) of the class {}//requires that the parameter can only use the object that implements the interface function f (arrary $p) {}// This parameter is required to use only the array function f (callable $p)//requires that the parameter is only a function (method), which is called a callback function (method)
<?phpclass a{}function F (A $p) {} $obj = new A (); F ($obj);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.