Basic PHP classes and objects

Source: Internet
Author: User

1. attributes, methods, constants

Attribute. A constant is used to save data.

Method used to store the executedCode

Note: you cannot directly obtain the attributes of an object in a method.

2. Access Control

Public, protected, and private

Public-defined class members can be accessed anywhere (inside the class and outside the class)

The Class Members defined by protected can be accessed by their subclass and parent classes (of course, they can also be accessed within the current class)

Private-defined class members can only be accessed by their classes

Raise the class's ability to control class members as much as possible

If you can use private members, try to use private members.

3. Class Constants

1. The definition of a class constant can only be defined by the const constant name = constant value

Const Path = '';

2. Obtain the constant: Class Name: constant name

3. Range Resolution OPERATOR: used to access static attributes, methods, and constants

4. automatically load objects

When a class is required, but this class is not available, the _ autoload () function is automatically implemented, so that the script engine has the last chance to load the required class before a PHP error occurs.

When calling this function, the currently required class name will be passed to the function as a parameter

5. constructor destructor

1. Constructor ()

Constructor. When an object of a class is instantiated, this method is automatically called first.

2. destructor (executed before the PHP Object is destroyed)

Destructor. This function is called when PHP objects are destroyed (script execution is complete ).

3. If you pass parameters when instantiating a class object, it means to pass parameters to the class constructor.

6. Object acquisition (clone)

1. The value assignment between objects is a reference transfer.

PHP has two ways to get new objects.

(1) instantiate an object through a class

(2) clone (obtain different objects) 7. object orientation 3 features

1. encapsulation type

Encapsulation refers to the process in which natural entities (features) are described in computer object-oriented languages and natural languages are abstracted into computer languages.

2. Inheritance

It is necessary to extract the same code, load it as needed, and implement it through class inheritance (leave a job and encapsulate common database operations to DB. Class. php)

3. Polymorphism

Polymorphism is an object-oriented feature, but it is not obvious in a weak language like PHP.

Polymorphism means multiple States of the same object.

The same method is used to calculate the area. The method of rectangle and square is different. This is polymorphism.

8. Static attributes

1. In object-oriented programming, we can access its members through object access methods and attributes, and through classes. Such methods and attributes are static

2. To define static attributes of a class, you need to add a static keyword.

3. Static attributes can be accessed in static methods or non-static methods.

4. Each instance of the class can access the static method defined in the category class and cannot access the static property because the static property belongs to the class

Case: Create an object counter (obtain the number of objects instantiated)

9. Design Mode

Factory model:

The reason is that the factory model is only responsible for the production object.

The most commonly used place is to dynamically create objects based on conditions.

Singleton Mode

We only need one database object (one connection is enough). When a class can be used once to complete the function, we need to ensure that one object is enough to be instantiated once.

10. Magic Methods

_ construct ()
_ destruct ()
_ tostring ()
An echo object is automatically called when an object is converted into a string.

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.