PHP5 class constraints

Source: Internet
Author: User
PHP5 type constraints: function parameters in PHP have type constraints. That is, the parameter type is explicitly required. For example, the following code :?? & Lt ;? The first parameter of the following class classMyClass {*** test function ** in php must be an OtherClass object * publicfunctiontest (OtherC PHP5 type constraint

Function parameters in PHP have type constraints. That is, the parameter type is explicitly required. For example, 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 {public $ var = 'Hello world' ;}?>

?

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

If you explicitly specify a parameter as a variable of a certain type, you can participate in fast PHP program efficiency. Why? Because, instead of objects, PHP5 is passed by reference by default. Therefore, public function test (OtherClass $ otherclass ){
In this line of code, although there is no reference symbol, it is actually a reference for passing parameters.

What about arrays? In PHP, the type of big data is similar to three types: objects, arrays, and strings. strings must explicitly use the reference symbol (&) in the parameter table.

How about the array, you can give it a try. So I'm not talking about it anymore!

?

When the object parameter is added with type constraints, the default value can still be used to become an optional parameter. In this case, the function must determine whether the input parameter is NULL. However, the instanceof operator is not required. (This operator is special. if it is to be negative, it must be? (! (A instanceod B .?) The original code with the default value is as follows:

?

 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 the object $ cbt-> test ('test', 'blue', $ cat); // we can pass the object, but the array still does not pass print_r ($ cat); $ cat1 = new ca (); $ arr = array (1, 2, 3, 4, 5 ); $ cbt-> test ('test', 'black', $ cat1, $ arr); // input all array objects to array_push ($ arr, 6, 7, 8, 9 ); // change the array print_r ($ cbt-> array); // The Returned array is not changed.?>

?

?

?

?

?

?

Dewei

In php5, objects do not need to be referenced (&), and large arrays need to be referenced.

Tonyseek

The type constraints mentioned by the author seem to apply to interfaces in PHP.

Third floor renzhen

Dewei wrote:

In php5, objects do not need to be referenced (&), and large arrays need to be referenced.


This depends entirely on the needs of your code. if you want to change the parameters inside the function and outside the function, use &; otherwise, no.
What if the parameter array in the function does not change?
In fact, adding and not adding does not matter. Some people may say that if you do not add an application, isn't it a value transfer? how much memory will be wasted for large arrays.
In fact, PHP 5 has implemented Zero on copy for the assignment. Simply put, if a large array a is assigned to B, it points to the same array object at the beginning, the memory will not multiply. only when B changes (adding an element and other operations) will the PHP underlying layer copy a new a Array and then perform the operation.
This mechanism greatly improves the performance of value assignment without affecting the performance.
For more information, go to the Internet.

Reeze on the 4th floor

There should be no impact on the efficiency, so we have to compare the results and increase the calculation for the type check.

The most obvious advantage is that it can constrain the use of interfaces and reduce the code for manual type checks. Interface Specifications

5 floor iminto

Bardo wrote:



If you explicitly specify a parameter as a variable of a certain type, you can participate in fast PHP program efficiency.



?


You must pay for your speech.

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.