PHP Object-oriented, syntax "access to Members: external and intra-method call Object keyword This", the scene of a constructor and a destructor

Source: Internet
Author: User

1. Object-oriented "a programming idea does not mean that the process is inherently high-end, is essentially a better extension of the maintenance of programming ideas," "programming thought itself does not compete with the points, but there will be a choice process"

Process oriented: Splitting a business logic into individual processes, implementing each process separately, and realizing the idea of business logic is process oriented. "Process-oriented problems: The abstraction of the overall logic of the business is always there." Process-oriented is a list of actions. But object-oriented is the description of the subject. Therefore, the process can not fully describe the business logic "essentially object-oriented more realistic life" through the main control "above is the essence of the difference and choice." "In order to be realistic, process-oriented needs to define too many subjects repeatedly. This is also the reason for object-oriented redundancy "

So object-oriented is a kind of programming idea that wants the computer to better simulate the reality "and because the body carries out the simulation logic, the advantage is that the module is then called the business separately for each object, thus achieving better separation and module" so object-oriented is the only way to solve complex logic and project. "This is why object-oriented is important: to insinuate real entities into computer programming languages is the object-oriented practice"

2. The basic grammar object corresponds to an abstraction of the real entity in the computer. Objects through class instantiation to achieve the "source of objects" class is the abstraction of a kind of thing "because objects rarely appear orphaned, and are based on a special case of a type of thing (overriding the object of a property in a class) or an instance (a simple instance) is an object"

Syntax: first declare the class class name {}//when defining a class using properties and methods to control the "member" object = new class name;//materialized, output only attribute list, no method information "No matter the visibility"//each instantiation of the class, Creates an object (which opens up a new space to hold the object, so that the operation between the different instance objects does not affect each other) (the output object var_dump the Output object)//The operation of the object is the principal operation "is also the main object-oriented programming" because of the need for object-based operations, So you need access to Object members: Access key (different ways of visibility and access) "

Access to members outside the class Access property: Object--property name; Access method: Object, method name (); "How to access members of this object in a method in a class" The This keyword refers to the current object to be able to access the Member "because the member is also a local scope in a method in a class $this, or the $this itself is also a local scope variable, However, the PHP mechanism will refer to the object that executes the method call "when a member of the object is called within a method and after it has been changed, it will be global in the lifetime of the entire object (as long as the value is called, the value is displayed after the change). Because of the global role of the object's properties on the object "" is precisely because of this mechanism, so that object-oriented programming in the processing of data can better simulate the reality of the subject "" Focus "

The visibility of members in the class "Public/protected/private"

"This is the basic object-oriented concept."

3. The constructor "__construct" "initializes the object when the class is instantiated" initialize the properties of the object "to initialize the properties of each class for the public to use, save a lot of work. "But cannot abuse" "in object as tag" "or public use of data will be placed in the constructor to initialize" "The parameters required for initialization here are the arguments that are passed in when the New keyword is used during instantiation. Initialization of constructors is handled for these parameters "

The required properties in the rest of the classes need to be defined only in the class "Instead of being defined in the constructor"

In addition, methods like method names and class names in a class also act as constructors, "but there is a problem with precedence." When both cases exist, the constructor uses __construct to perform the constructor function. There is a possibility of conflict (completely eliminating the use of a method consistent with the class name in a Class) "

The "instance in the real" constructor is only used to initialize information about the city in the company's instance "the rest of the properties on the cache are placed in the parent class for subclass inheritance"

4. After the destructor "__destruct" object is destroyed when the method is called "object is destroyed" unset ", the object is actually occupied by the resource is not destroyed. Therefore, destructors are used to destroy additional resources. "Save Resources" but this operation of destroying objects is not used by itself.

When the object is destroyed: (1) After the user script is finished, all resources including the object will be destroyed "will be triggered, but no meaning" (2) the Unset object (3) will save the current object's variable assignment can other values "will also trigger" (even if the re-assignment itself object will trigger)

is the "problem" because the MySQL connection such resources can be preserved, so the singleton mode of the application scenario?

PHP Object-oriented, syntax "access to Members: external and intra-method call Object keyword This", the scene of a constructor and a destructor

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.