PHP closure instance parsing, php closed instance

Source: Internet
Author: User

PHP closure instance parsing, php closed instance

This article analyzes the concept machine usage of closures in PHP programming and shares it with you for your reference. The specific analysis is as follows:

In general, closures are PHP anonymous functions, but unlike functions, closures can use the value of the variable in the scope of the function declaration when using the function declaration.

The specific form is as follows:

$ A = function ($ arg1, $ arg2) use ($ variable) {// declare the function closure to the variable $ a. The parameter is $ arg1, $ arg2, the closure must use the $ variable}

The usage example is as follows:

<? Php $ result = 0; $ one = function () {var_dump ($ result) ;}; $ two = function () use ($ result) {var_dump ($ result) ;}; // The variable $ two itself records the declaration of the function and the value of the variable used by use $ three = function () use (& $ result) {var_dump ($ result) ;}; $ result ++; $ one (); // outputs NULL: $ result is not in scope $ two (); // outputs int (0): $ result was copied $ three (); // outputs int (1)?>

I hope this article will help you learn PHP programming.


Php instantiation

Several writing errors have been corrected!
$ Arr = $ this-> GetRecord ($ SQL); // isn't this a method in the parent class called?
I told you what inheritance means that the subclass has all the members of the parent class (where inheritance is allowed). You can use the scope connector to use the parent class members, you can also use the $ this keyword (static, private, not !!!!!)!!!!!!!

How to Understand object-oriented programming:
Here is an example:
Class student {
Public $ name;
Public statics $ age;
Public function set_name ($ name ){
$ This-> name = $ name; // this is correct
Self: $ name = $ name; // correct
$ Name = $ name; // This sentence is incorrect.
}
Public function set_age ($ age ){
$ This-> age = $ age; // Error
Self: $ age = $ age; // correct
}
}
// Why? That is to say, if you want another member in the class, you need to pass
// $ This keyword or scope parser, but if it is a static member, it cannot be referenced by $ this access, and cannot be used directly like normal code.
$ This-> name: You can understand it as follows: The Student name, that is, $ this represents the class name.

Class boy_student extents students {
// This class is extended from students, that is, it has all students class members,
// Of course, you can also add a member that students does not have.
$ Aadress;
:
:
:
// If you want to apply the method of the parent class: You can do this:
Parent: set_name (....)
// Of course, all the things involved here can be written into a book. It doesn't mean that some parent class members can access sub-classes, and some even cannot even inherit. This involves the visibility issue!
}

*********************************
$ This-> is xxx instantiated for calling?
*******************************
This is not instantiation. What is instantiation? That is to say, what we just defined is an abstract thing, just like a student, which is a class;
$ Zhangshan = new students ();
This is profit-making. Do you understand!

Php instantiation

: It is a static access method. It is only related to the class and has no relationship with the Class Object. Therefore, the direct call is the class name: method name.
Because the PHP language itself is a language that is not rigorous enough (or in other words, it is more flexible), when a method is public and does not use $ this representing an object in the method, it can be used:: To be called. php automatically parses and calls it using static methods during parsing. Of course, after PHP5, the static public function func () {} method is introduced. Therefore, if PHP5 has a complete error reporting mechanism, when a method without static declaration is called directly, A notic will be prompted ···
We recommend that you do not use this unless you declare a static method!

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.