Home > Backend Development > PHP Tutorial > Learn about object-oriented concepts in PHP

Learn about object-oriented concepts in PHP

青灯夜游
Release: 2023-04-08 14:00:02
forward
2977 people have browsed it

This article will talk to you about the object-oriented concept of PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Learn about object-oriented concepts in PHP

1. The concept of object-oriented

1. Object-oriented realizes the three aspects of software engineering Goal: Reusability, flexibility and scalability

2. Make the programming code simpler, easier to maintain, and have stronger reusability

3. Object-oriented characteristics :

Encapsulation: Hide the properties and implementation details of an object

Inheritance: The mechanism for deriving one or more classes from a base class

Polymorphism: Depending on the context in which the class is used To redefine or change the nature or behavior of a class

2. The relationship between classes and objects

1. Classes are used to generate objects Code template

2. In PHP, use the keyword class and an arbitrary class name to declare a class, with the first letter capitalized

3. Objects are "instances" of classes, and classes are objects. Abstract

	//声明一个类
	class Book{
	
	}
	//实例化一个对象
	$book = new Book();
Copy after login

3. Attributes in the class

1. Refers to the variables declared in the class, also called member variables, used To store different data between objects

2. Use of attributes: Usually use the "->" symbol to connect objects and attribute names to access attribute variables

3. In class methods The attributes of the same object are accessed internally through "$this->"

4. The variable pointing to the object is a reference variable, and the memory address of the pointed object is stored in this variable

//声明一个类
class Book{
public $name='PHP学习';
public function getName(){
return $this->name;
}
}
//实例化一个对象
$book = new Book();
//访问类的对象成员
echo $book->name;//输出 PHP学习
//访问类的对象方法
echo $book->getName();//输出 PHP学习
Copy after login

4. Methods in the class

1. Properties allow the object to store data, and methods in the class allow the object to perform tasks

2. Use of methods: Use the "->" symbol to connect the object and method name to call the method, followed by parentheses

3. If the number of parameters exceeds the number of parameters defined by the method, PHP will ignore the excess Parameters, no error will be reported

4. Allowing a reference to another object to be passed inside a method

5. Construction method

1. The constructor method is a method automatically called when an object is created to ensure that necessary attributes are set

2. Use __construct() to define the constructor

//用来初始化一个成员变量
public function __construct(){
$this->name = 'PHP学习';
}
Copy after login

6. Destructor and PHP’s garbage collection mechanism

1. The destructor method is executed when an object becomes garbage or when the object is explicitly destroyed. method.

2. When no variable refers to this object, the object becomes garbage, and PHP will automatically destroy it in memory. Garbage disposal mechanism

3. When a PHP thread ends, All memory space currently occupied will be destroyed, and all objects will also be destroyed

4. Use __destruct() to define the destructor. Generally, do not define the destructor

5. When When the object has no reference, the object is also destroyed

This article is reproduced from: https://blog.csdn.net/kelinfeng16/article/details/82662288

Recommended: PHP video tutorial

The above is the detailed content of Learn about object-oriented concepts in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template