Home  >  Article  >  Backend Development  >  How to define a class in php? How to define a class in php

How to define a class in php? How to define a class in php

不言
不言Original
2018-08-22 15:29:269909browse

The content of this article is about how to define a class in PHP? The method of defining a class in PHP has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Basic concepts of object-oriented

Includes 3 parts:

Object Oriented Analysis (OOA)

Object-oriented design ( Object Oriented Design, OOD)

Object Oriented Programming (OOP)

Definition of class

A class is a collection of attributes and methods [human, animal, Plant class】

Attributes are variables defined within the class, also known as member attributes and member variables.

Methods are functions defined within a class.

What do you need to learn?

How to define a class?

<?php
//关键词 class  +  类名称
class person(){

}

How to instantiate a class?

<?php
class person(){

}
//对象=关键词  类名();
$person=new person();

How to call a method in a class?

<?php
class person(){    
    function run(){
        echo "我在跑步";
    }
}
$person=new person();
$person->run();//运行结果:我在跑步
//对象->类中的方法

How to retrieve variable information in a class?

<?php
class person(){    
    public $name="郝云";
}
$person=new person();
echo $person->name();//运行结果:郝云
//对象->类中的变量名(注意有无$)

Definition of object

An object is a specific instantiated entity

The relationship between classes and objects

What are the modifiers of variables?

public: Public, the attribute can be used outside the class

protected: Protected, the attribute can only be used inside the class (if there is inheritance, it can be used inside the subclass )

private: private, can only be used inside the current class, and cannot be used anywhere else

Note: If there is no method in front, it defaults to the PUBLIC modifier;

Three major characteristics of object-oriented

Encapsulation, inheritance, polymorphism

Encapsulation, also known as information hiding, classes only retain limited interfaces and external connections. Know how to call a method without worrying about the details of how to implement it.

Inheritance, the derived class automatically inherits the properties and methods in the parent class, improving code reusability. extends

Polymorphism means that different objects of a class can obtain different results by calling the same method. Enhanced system flexibility and reusability.

After-school homework:

Define a student class Student

  1. ## Define attributes: number (id), name (name), gender (sex) .

  2. Define the public calling method: say() Output I am Zhang San, male, number 004

Related recommendations:

A simple PHP custom exception class

How to define a class in PHP

The above is the detailed content of How to define a class in php? How to define a class in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more