class constraints of PHP5

Source: Internet
Author: User
Type constraints for PHP5

function parameters in PHP have type constraints. That is, the type of the parameter is explicitly required. such as the following code:

?

?

 
  var;    }    /**     * Another test function     * The first parameter must be an array */public    function test_array (array $input _array) {        Print_r ($input _ Array);}    } Another class Otherclass {    

?

So what's the relationship between type constraints? There's a peculiar problem here. That is:

If you explicitly specify that the parameter is a variable of some type, then participate in the fast PHP program efficiency. Why is it? Because, anti is an object, PHP5 is passed by reference by default. So, public function test (Otherclass $otherclass) {
In this line of code, although there is no reference to the symbol, the actual reference is also the argument.

So, what about the array? Because, in PHP we face the type of big data is three classes, objects, arrays, strings, and strings are to be explicitly used in the parameter table of the reference symbol (&).

The array, you can try to understand. So I don't have to say much!

?

When an object parameter is added to a type constraint, it can still have a default value, which becomes an optional parameter. If so, the inside of the function is to determine whether the passed parameter is null. However, it is generally not necessary to use the instanceof operator. (This operator is special, if you want to negate, must be?) (! (A Instanceod B)) form.) The following are the original codes with the default values:

?

 
  Color= $color;        $this->ca = $ca;        $this->array = $array;        $ca->name= $srcStr;        Print_r ($this);    }} $cat =new CA (); $CBT =new CB (); $CBT->test (' Test ', ' red '); Here we can not pass object $cbt->test (' Test ', ' Blue ', $cat); We can pass the object, but the array still does not pass Print_r ($CAT); $cat 1=new CA (), $arr =array (1,2,3,4,5), $CBT->test (' Test ', ' Black ', $cat 1, $arr); Array objects are all passed in Array_push ($arr, 6,7,8,9); Then change the array print_r ($CBT->array); The result is that the array passed in is unchanged.?>

?

?

?

?

?

?

1/F Dewei 2011-01-25

In PhP5, objects are not required to be referenced (&), and large arrays are required to be referenced (&).

2/F Tonyseek 2011-01-26

Landlord said the type of constraint seems to apply to PHP interface

3/F Renzhen 2011-05-20

Dewei wrote

In PhP5, objects are not required to be referenced (&), and large arrays are required to be referenced (&).


This completely depends on your code needs, if you want the function inside the parameter change and the function also change, then use &
What if the parameter array inside the function doesn't change?
In fact, it doesn't matter if we add or not. Some people may say, the non-application is not the value of the transfer, the large array will waste much memory ah.
In fact, PHP 5 has implemented a zero on copy of the assignment, the simple point is, if you assign a large array A to B, at first they point to the same number of objects, memory does not multiply, only if B changed (plus an element and so on), PHP will first copy a new array of a, And then do the operation.
This mechanism greatly improves the performance of value-transfer assignment, and does not affect performance at all.
Detailed information can be found on the Internet.

4/F Reeze 2011-06-02

There should be no impact on efficiency, not to contrast, the type of inspection has increased the calculation.

The most obvious benefit is the ability to constrain the use of the interface and reduce the manual type checking code. Ability to standardize interfaces

5/F Iminto 2011-06-27

Bardo wrote

If you explicitly specify that the parameter is a variable of some type, then participate in the fast PHP program efficiency.



?


You have to pay for what you say.

  • 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.