Home > Backend Development > PHP Tutorial > Object-oriented in PHP

Object-oriented in PHP

墨辰丷
Release: 2023-03-25 19:38:01
Original
2901 people have browsed it


This article mainly introduces object-oriented in php. Interested friends can learn about it. I hope it will be helpful to everyone.

1. Basic concepts of object-oriented


Object-oriented includes 3 parts: Object Oriented Analysis , OOA), Object Oriented Design (OOD) and Object Oriented Program (Object Oriented Program), the two key concepts of object-oriented are classes and objects.

Class:

A class is a collection of variables and methods that act on these variables.

Object:

The object is the product of instantiation of a class and is an entity.

The three major characteristics of object-oriented programming

Encapsulation, inheritance, and polymorphism.

2. Classes and Objects

Definition of classes

/**
 * 定义类,继承AnotherClass
 */
 class MyClass extends AnotherClass
 {
 	
 	function __construct(argument)
 	{
 # code...
 	}
 }
Copy after login

Instantiation of classes

 $user = new User("愤怒的小水滴", 16);
 echo $user->name."<br>".$user->age;
Copy after login
class Student extends User
{
    /* 构造函数 */
    public function __construct($name, $age, $school)
    {
        parent::__construct($name, $age);
    }
    /* 析构函数 */
    public function __destruct()
    {
        parent::__destruct();
    }
}

 $student = new Student("愤怒的小水滴", 16, &#39;hebei&#39;);
 echo json_encode($student)."<br>";
Copy after login



Variable declarators can be public or private , protected, static, final.

Related recommendations:

php object-oriented transaction script mode

php Detailed explanation of commonly used keywords and magic methods in object-oriented

PHP object-oriented final class and final method

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

Related labels:
source:php.cn
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