Article From:

The following is the result of the page output;


 The object oriented is the “sublimation” of the design thought – the process oriented is the natural thinking process to solve the simple problem, and the object oriented is the good idea to solve the large-scale complex problem.

1,Define classes; there are only 3 kinds of code in defining class syntax:

1.1Define attributes (variables)

1.2Definition method (function)

1.3Define constant (class constant)

The value of the object

phpIn the case of variable values, there are 2 methods:

Value transfer: when passing, it copies the data itself. The default is value transfer.

Results: after passing, there were 2 identical data, but the two variables were independent.

Citation transfer: when passing, the reference relationship is copied. The use of “&” can be used to achieve reference transfer.


Class members

The interior of a class can have 3 kinds of code:



Class Constants

They are collectively referred to as “members of the class”

varIt’s an alias for “public”, which is better with public.

Static property

In contrast: a general attribute, his value is a specific object attached to the class (although it is defined in a class), or, in other words, the values of the same attributes of each object, may be different.

The static attribute is that it belongs only to the class itself, and can be regarded as the “common data” of all objects.

The use of static properties:

Class:: $static attribute name;

Compare the use of common attributes:

Object -> common attribute name; / / common attribute can also be called “instance attribute”, and the object is also called “instance”.

Static method

If the static keyword is modified in front of a method, it becomes a static method.

Static methods are also subordinate to classes rather than specific objects. The use method is similar to the static property:

Class name:: static method name ();

From a conceptual point of view, it can be considered that static methods belong only to classes but are shared by all objects.

 If a static method is invoked through a class name, the $this keyword can not appear in the method.

Construction method

1,The name of the method is fixed, __construct ();

2,This method must be a common method (not static).

3,Usually this method should be public

4,Usually, $this is used to assign attributes to the method in this method.

5,When the new class name () is called, it is actually invoking the construction method.

6,If a construction method is defined in a class, the method is called when instantiating the class, and the parameters of instantiation need to match the parameters of the construction method.

1,The deconstruction method is usually not much needed to define.

2,The deconstruction method can not be called.

3,The method of deconstruction can not have a parameter.

4,The destructor method can be used to clean up some data that can not be cleaned after the PHP code is finished, such as the generated file.

Several cases of the destruction of the object:

The script program runs and ends automatically, and a unset () object variable is destroyed, and the value of the object variable is destroyed.

Link of this Article: PHP class and object

Leave a Reply

Your email address will not be published. Required fields are marked *