Home  >  Article  >  Backend Development  >  Object-oriented practice in PHP - basic practice cases

Object-oriented practice in PHP - basic practice cases

易达
易达Original
2020-05-21 17:44:442015browse

Object-oriented practice in PHP - basic practice

(1) Basic practice

1. The concept of class

Object-oriented practice in PHP - basic practice cases

1. Common points:

all have the same attributes

all There are the same methods

2. Differences:

The specific values ​​are different

3. Summary: They are all Objects of a class

Speaking of classes, let’s talk about the concept of classes

1) Class definition

-We say that things flock together and people divide into groups. Classify objects with similar characteristics into a class

So when we look at objects, try to compare their properties and methods. If their properties and methods are very similar, we can put They are classified into one class

-The class defines that these similar objects have the same properties and methods

For example: an NBA player is the definition of a class, which stipulates that as long as it is an NBA Players must have: age, name, height, weight, team, player number

and then they will also have the same skills:

Object-oriented practice in PHP - basic practice cases

But the question is, is there a specific height defined in this class, is there a specific name, is there a specific weight, none of them, but if we write all these attributes, will it become a specific one? Object

This is the relationship between classes and objects. Specifically, a class is a description of similar objects. This is called the definition of a class. A class is the blueprint or prototype of all these similar objects

For example, how did the object James come from? Just set the attribute value of this class.

The object of the class is an <strong>instance(Instance)</strong> of the class. This operation is also called instantiation of a class.

For example, James, Jordan, and Kobe are all instances of the NBA player class. The process of instantiating them is called instantiation of a class.

For classes, we can The understanding is that it is just an empty shelf. It only defines how objects are divided. As for objects, it is defined. For specific values, all attribute values ​​must be written in detail.

Supplement A concept: We collectively refer to the objects and methods of a class as class members

2. The concept of instantiation

1) Definition of instantiation of a class

is to create a specific object through a class. This process is the instantiation of the class, which is equivalent to filling this empty shelf.

Example: For example, through the NBA player class we created James and Kobe Bryant , Jordan, among which the NBA player class was instantiated three times

Object-oriented practice in PHP - basic practice cases

Summary: There is only one class, but it can be instantiated multiple times to obtain multiple specific Object

Tip: How to implement it, we will demonstrate it in the code later

2) Instantiation of the class

2) Example: Take Jordan as an example

Object-oriented practice in PHP - basic practice cases

The left side is a class with no data, and the right side is Jordan. So how to instantiate it is to directly add these names, height, weight, team, and players If the values ​​​​of the numbers are filled in specifically, then the Jordan object will be instantiated

Tip: Instantiation can be understood as making the class into a practical example

3. Specific cases

1) Case study objectives

1. How to define a class

2. How to instantiate a class

3. How to call a class Properties and methods

2) See the following code for details

<?php
/***
 * 案例目标:
 *  1.类的定义
 *  2.类的实例化
 *  3.属性的赋值
 *  4.方法的调用
 *  
 */
/***定义一个类,类的名字叫NbaPlayer(中文=NBA球员)
 * 提示:
 *  1.定义类的过程:我们首先以class开始,然后写上类 名称:NbaPlayer,最后要写上一对{}
 *  2.类的名字通常首字母要大写
 *  3
 * 
***/
class NbaPlayer{
    //定义属性
    public $name = "";
    public $height = "";
    public $weight = "";
    public $team = "";
    public $playerName = "";
    //定义方法
    //定义跑的方法
    //提示:
      //方法定义的过程:
        //1.写上方法的类型,public,表示公共的方法,可以被外部直接调用
        //2.写上function
        //3.定义方法的名称,然后写上一对(),最后{}结尾     
    //总结:
        //方法定义和之前js中函数的定义是一样的,只是类中的方法多了一个public
    public function run(){
        //里面的代码我们称之为业务逻辑
        echo "跑步<br/>";
    }
    //定义跳跃方法
    public function jump(){
        echo "跳跃<br/>";
    }
    //定义运球方法
    public function dribble(){
        echo "运球<br/>";
    }
    //定义投篮的方法
    public function shoot(){
        echo "投篮<br/>";
    }
    //定义扣篮方法
    public function dunk(){
        echo "扣篮<br/>";
    }
    //定义传球
    public function pass(){
        echo "传球<br/>";
    }
} 
//类到对象的实例化
/*总结:
    1、类的实例化过程= 通过new 类名() 即可完成一个类的实例化过程
*/
//1.创建乔丹
$jordan = new NbaPlayer();//类的实例化
//类赋值
$jordan->name = "乔丹";
$jordan->height = "1.98米";
$jordan->weight = "98公斤";
$jordan->team = "公牛";
$jordan->playerName = "23";
//输出对象值
print_r("乔丹名称:".$jordan->name."<br/>");
print_r("乔丹身高:".$jordan->height."<br/>");
//总结:通过->可以调用对象里的属性
echo "<br/>";
//输出对象方法
$jordan->dribble();
$jordan->pass();
//总结:通过->符号可以调用对象的方法
?>

The above is the detailed content of Object-oriented practice in PHP - basic practice cases. 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