


What are classes and objects in PHP? Why learn object-oriented? how to use?
The previous article introduced to you "What is pdo in PHP? What are the advantages of accessing a database? What does extension do? 》, this article continues to introduce to you what are classes and objects in PHP? Why learn object-oriented? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
PHP Advanced Syntax - Classes and Objects
1. Why learn object-oriented
Object-oriented thinking is a highly abstract form of human thought.
After learning object-oriented, our code will be very elegant and compact
We only need one or two lines of code to complete some functions
2 , process-oriented and object-oriented
The codes we wrote before were all process-oriented
Building a house:
1. Laying the foundation
2. Building a house
3. Decoration
Object-oriented thinking:
1. New an object, the object calls the foundation method
2. new A contractor, let him build a house
3. new-a decoration worker, let him decorate
Everything is an object, and multiple objects work together to complete our function.
In the future, our thinking must change to object-oriented.
Syntax level: encapsulation, inheritance, polymorphism
Ideological aspect
3. Classes and objects in life
Human beings :Abstract concept
Object: Kobe, Wade
Class is an abstract concept, object is the concrete thing
Car Your Land Rover
Computer Your Computer
Official Concept: A class is the abstraction of an object, and an object is the concrete representation of a class
4. Simple use of classes
Attributes and Behavior
Property===》Variable
Behavior===》Method
Naming specification
Class names follow the big camel case principle
persontest ====> PersonTest====> personTest
Create object method
//Create an object through the new keyword, after person You can add parentheses or not. It is recommended that everyone
add
//The first method of object creation
$xiaoming = new Person() ; //var_dump ($xiaoming) ;
//The second method of object creation, Create objects through class name strings
$className ='Persoin' ; $xiaoli = new $className () ; var_dump($xiaoli) ;
Object access properties and methods
$xiaoming->name = '小明' ; $xiaoming-> love () ;
For the simple use of classes, we write through PHP syntax, first starting with class as the keyword, We write the class name with person. At this time, we will have attributes and methods in the class. Let’s take the code as an example:
<?php class Person { public $age; public function like( ) { echo '我喜欢你'; } } $xiaoming = new Person() ; var_dump($xiaoming) ; ?>
The code displays the result:
<?php class Person { public $age; public function like( ) { echo '我喜欢你'; } } //$xiaoming = new Person() ; $name = 'Person'; $xiaoming = new $name(); //var_dump($xiaoming) ; $xiaoming->like(); ?>
Recommended learning: php video tutorial
The above is the detailed content of What are classes and objects in PHP? Why learn object-oriented? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses the mysqli_query() and mysqli_fetch_assoc() functions in PHP for MySQL database interactions. It explains their roles, differences, and provides a practical example of their use. The main argument focuses on the benefits of usin
