This article mainly introduces object-oriented in php. Interested friends can learn about it. I hope it will be helpful to everyone.
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.
Definition of classes
/** * 定义类,继承AnotherClass */ class MyClass extends AnotherClass { function __construct(argument) { # code... } }
Instantiation of classes
$user = new User("愤怒的小水滴", 16); echo $user->name."<br>".$user->age;
class Student extends User { /* 构造函数 */ public function __construct($name, $age, $school) { parent::__construct($name, $age); } /* 析构函数 */ public function __destruct() { parent::__destruct(); } } $student = new Student("愤怒的小水滴", 16, 'hebei'); echo json_encode($student)."<br>";
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!