Home > Backend Development > PHP Problem > Should PHP be object-oriented?

Should PHP be object-oriented?

王林
Release: 2023-02-25 13:46:02
Original
2523 people have browsed it

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 类名{
    //属性
    //方法
    //常量
}
Copy after login

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();
Copy after login

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!

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