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!