Home > Article > Backend Development > Should PHP be object-oriented?
1. Introduction to object-oriented
Object-oriented is a programming idea. Programming ideas are process-oriented and object-oriented
Process-oriented: Programming ideas focus on the process
Object-oriented: Programming ideas focus on the participating objects
II , The benefits of object-oriented
1. Multi-person cooperation
2. Reduce code redundancy and high flexibility
3. Maximize the reusability of code To the extreme
4. Strong scalability
3. Classes and objects
1. Objects are concrete things that exist, and objects are composed of attributes. and methods
2. A class is a collection of objects with the same properties and behaviors
Note: A class can create multiple objects
Summary:
1. Objects are composed of properties and methods
2. Classes are a collection of the same properties and methods of all objects
3. When developing, first write classes, create objects through classes, and call methods and properties through objects
4. Implement classes and objects in PHP
4.1 Create classes
Syntax:
class 类名{ //属性 //方法 //常量 }
A class is composed of attributes, methods, and constants. It can also be said that class members include: attributes, methods, and constants.
Naming rules for class names:
1. Start with letters and underscores, followed by letters, numbers, and underscores
2. Cannot Use PHP keywords to make class names
3. Class names are not case-sensitive (variable names are case-sensitive, keywords and class names are not case-sensitive)
4. Class names use Pascal nomenclature (Big camel case capitalizes the first letter of the word)
4.2 Object instantiation
instantiate the object through the new keyword
<?php //定义类 class Student { } //实例化对象 $stu1=new Student();
4.3 Comparison of objects
Note: The transfer of objects is address transfer
Equal: The structure and the saved value are equal if they are the same
Congruent: Only when they point to the same object Are congruent
5. Attributes
The essence of attributes is variables. Call the members of the object through ->object name->property name, object name->method name ().
6. Method
The essence of the method is the function
Summary:
1. Before the method public can be omitted, if omitted, the default is public.
2. Public in front of the attribute cannot be omitted
Recommended video tutorial: PHP video tutorial
The above is the detailed content of Should PHP be object-oriented?. For more information, please follow other related articles on the PHP Chinese website!