Home  >  Article  >  Backend Development  >  Let’s talk about the basics of object-oriented programming in PHP (1)

Let’s talk about the basics of object-oriented programming in PHP (1)

little bottle
little bottleforward
2019-04-23 17:05:481816browse

This article mainly talks about the foundation of PHP object-oriented programming (1), which has certain learning value. Interested friends can learn about it.

The basis of process-oriented is a sentence of code, while the basis of object-oriented is objects, and objects are derived from instances of classes.

The definition of a class: a collection of things with the same attribute definitions and behavior.

A class is a collection of variables (variable attributes) and functions (class methods) that act on these variables. Attributes and methods are the basis of a class.

1. Class encapsulation

A class is a collection of variables and functions that act on variables, so creating a class cannot be separated from variables and functions.

Adding attributes to a class means adding new variables to the class, which can be defined using the public, protected, and private keywords. Variables modified with public can be accessed outside the class, while variables modified with protected and private cannot be accessed outside the class.

Adding a method to a class means adding a function to the class, and calling a class method means executing the function. To add methods to a class, just add a new function to the class.

If you want to reference the properties or methods of the class itself in the function, you must use the pseudo variable $this plus the name of the referenced property or method to achieve the function.

2. Class inheritance

Usually you need classes that have the same variables and functions as other existing classes.

A class that is extended or derived has all the variables and functions of the base class or parent class, and includes all defined parts of the derived class. At the same time, extended classes always rely on a single base class, that is, multiple inheritance is not supported.

Syntax: class Subclass extends Parent class

It should be noted that properties and methods modified with public and protected can be inherited by subclasses, while properties and methods modified with private cannot be inherited by subclasses.

3. Overloading of classes

The attributes or methods in a subclass sometimes have the same name as the attributes or methods in the parent class it inherits. At this time, the class overloading occurs. Overload.

Overloading of classes is actually the overloading of class attributes and class methods.

Of course, you can also access methods in the parent class in the subclass, but you need to use the two special keywords self and parent, which are used to access members or methods inside the class. .

$this is a pointer to an object instance, which is determined during instantiation;

self refers to a reference to the class itself, and generally self points to a static variable in the class. Format: self::static variable name

Parent is a reference to the parent class. Generally, parent is used to call the constructor of the parent class. .

4. Functions related to classes and objects in PHP

class_exists() function return type: Boolean value

Function: This function checks whether the class has been defined

Get_class_methods() function return type: array (all method names)

Function: This function returns an array composed of class method names

get_class_vars() function return type: array ( All public attributes of the class)

Function: This function will return an associative array composed of the default public attributes of the class, in the form: varname=>value

The get_class() function return type: character String

Function: This function will return the name of the class to which the object instance belongs

get_declared_classes() function return type: Array

Function: This function will return the name of the class specified by the current script An array of names of classes defined in .

Get_object_vars() function Return type: array

Function: This function returns an associative array composed of object attributes.

Get_parent_class() function Return type: String

Function: This function returns the parent class name of the object or class.

Return type of is_subclass_of() function: Boolean value

Function: Determine the relationship between the object and the class

Method_exists() function Return type: Boolean value

Function: This function checks whether the method of the class exists.

Related tutorials: PHP video tutorial

The above is the detailed content of Let’s talk about the basics of object-oriented programming in PHP (1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete