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

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

Jun 17, 2021 am 11:19 AM
classes and objects

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)