Home  >  Article  >  Backend Development  >  What are the key points of object-oriented php

What are the key points of object-oriented php

王林
王林Original
2019-09-26 18:05:272484browse

What are the key points of object-oriented php

1. What is $this

The object instantiated by the current class

2. Access the object Member

Object->Member

3. Constructor method

is usually used to initialize the properties of the object without writing the properties Dead, different objects have different attributes

4. Usage of get and set

Usually set the attributes to protected and use the getXxx and setXxx methods to set them and obtain attribute values ​​to avoid arbitrary external changes, and at the same time, objects can be filtered.

5. Class encapsulation

Public methods can be accessed outside the class, which is equivalent to the interface provided to the outside world

Private methods can only be accessed outside the class Inside (that is, within the curly brackets of the class), it cannot be accessed directly from the outside and can only be called through other public methods. Just like the switch of a computer, you can turn it on by pressing the power button.

protected, the same as private, but can be inherited in other classes.

6. Class inheritance and rewriting (extends)

The inherited class can access the public and protected methods or properties of the parent class, avoiding code rewriting .

To override the method of the parent class, the inherited class only needs to take the same method name as the parent class and write your own code in it.

7. Abstract class (abstract)

Abstract classes are used for inheritance and cannot be instantiated directly. Abstract methods do not need to write method bodies

The function is to rigidly stipulate the methods that subclasses need to implement. If they are not implemented, they cannot be instantiated.

8. Interface

Inherit and use implementations

Ensure code consistency

9. Namespace and automatic loading (namespace)

Avoid duplicate name conflicts and avoid require operations before each use of a class

There is namespace file location before declaring the class

namespace app/Admin;

There is use namespace/class name before using the class

use app/Admin/User;

10. Static variables and static methods

can be regarded as global methods

can be used without instantiating objects

Usage: Class name:: Member

11. const variable

const is mainly used for some rigidly specified data and conventional data codes, which can be found in Used outside the class, it can only be modified at the definition. For example, our http status code

definition: const ABC=12;

Usage: class name: :ABC;

Recommended tutorial:

PHP video tutorial

The above is the detailed content of What are the key points of object-oriented php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn