What are classes and objects in PHP? Why learn object-oriented? how to use?

慕斯
Release: 2023-03-10 17:02:01
Original
2406 people have browsed it

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.

What are classes and objects in PHP? Why learn object-oriented? how to use?

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
Copy after login

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

//The second method of object creation, Create objects through class name strings

$className ='Persoin' ;
$xiaoli = new $className () ;
var_dump($xiaoli) ;
Copy after login

Object access properties and methods

$xiaoming->name = '小明' ;
$xiaoming-> love () ;
Copy after login

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 &#39;我喜欢你&#39;;
        }
}
$xiaoming = new Person() ;
var_dump($xiaoming) ;
?>
Copy after login

The code displays the result:

What are classes and objects in PHP? Why learn object-oriented? how to use?

<?php
class Person 
{
        public $age;
        public function like( )
        {
            echo &#39;我喜欢你&#39;;
        }
}
//$xiaoming = new Person() ;
$name = &#39;Person&#39;;
$xiaoming = new $name();
//var_dump($xiaoming) ;
$xiaoming->like();
?>
Copy after login

What are classes and objects in PHP? Why learn object-oriented? how to use?

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!