Home > Backend Development > PHP Tutorial > Detailed explanation of object-oriented constructor in PHP

Detailed explanation of object-oriented constructor in PHP

易达
Release: 2023-04-08 17:02:02
Original
2269 people have browsed it

Object-oriented practice in PHP - basic practice

-Constructor number

1. Case objectives

(1), understand the constructor

a. Understand the definition of constructor

b. Understand the role of creating a constructor

c. Master the characteristics of constructor

d. Master the functions of constructor with parameters Notes

e. Create a constructor with default value parameters

(2), Supplementary knowledge

a. How to run a php file in the command line

b. The most basic command in Cmd

c. How to solve the problem of garbled characters in the command line

d. If the php file wants to display the line break effect in the command line, you need to write\n ,
It can only be recognized as a newline in the browser

1. The specific code corresponding to the constructor

Instructions: In order to ensure a Knowledge points can be more easily understood by everyone, so each article is closely connected and step-by-step. Therefore, the code this time is based on the previous article, but the focus of this code is to explain the constructor

<?php
/***
 * 案例目标:
 *  1.理解构造函数的定义和作用
 *  2.掌握构造函数的创建
 *  2.了解构造函数的特点
 *  3.带参数的构造函数的注意事项
 *  4.函数参数的默认值设置
 *  
 */
/***定义一个类,类的名字叫NbaPlayer(中文=NBA球员)
 * 提示:
 *  1.定义类的过程:我们首先以class开始,然后写上类 名称:NbaPlayer,最后要写上一对{}
 *  2.类的名字通常首字母要大写
 *  3
 * 
***/
class NbaPlayer{
    //定义属性
    public $name = "";
    public $height = "";
    public $weight = "";
    public $team = "";
    public $playerName = "";

    //构造函数
    
    /*
    定义: 它是一个特殊的函数,在构建对象的时候自动执行
    作用:初始化对象,在通俗一点讲就是给对象设置默认的属性或者默认的行为
        (变量初始化的意思就是给变量设置初始值)
    特点:1. 初始化对象的时候会自动触发的一个函数,不需要手动调用都会第一个执行的函数
          2. 不能自定义名称 只能命名为 __construct
          3. 如果函数带有了参数,你又没有设置默认值,那么使用的时候就需要传递参数进来,否则会报错
     总结:
        1.构造函数创建过程 = public  function __construct(){}
    */
    // 没有参数的构造函数
    /* public function __construct(){
         echo "构造函数触发了<br/>";
     }*/

    //如果我们希望通过构造函数来初始化对象,我们可以让构造函数带有参数
    public function __construct($name,$height,$weight,$team,$playerName){
        $this->name = $name;
        $this->height = $height;
        $this->team = $team;
        $this->playerName = $playerName;
        //知识点:在php里,我们在类里使用$this,那么这个$this就叫做伪变量,用来表示是对象本身
        //          既然是对象本身,那么我们就可以通过$this->的方式访问对象里的属性和方法
        
    }
    //定义方法
    //定义跑的方法
    //提示:
      //方法定义的过程:
        //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";

//通过带有参数的构造函数初始化对象
$jordan = new NbaPlayer("乔丹","1.98米","98公斤","公牛","23");
//输出对象值
print_r("乔丹名称:".$jordan->name."<br/>");
print_r("乔丹身高:".$jordan->height."<br/>");
//总结:通过->可以调用对象里的属性
echo "<br/>";
//输出对象方法
$jordan->dribble();
$jordan->pass();
//总结:通过->符号可以调用对象的方法


?>
Copy after login

2. Detailed introduction to supplementary knowledge points

1. Question: How to run a php file in the command line

1. Set environment variables (The following is the configuration of win10 system)

Detailed explanation of object-oriented constructor in PHP

##2. Right-click the computer icon on the desktop and select Properties

                       

Detailed explanation of object-oriented constructor in PHP

#3 . Click the advanced system settings

4. Open the environment variables

5. Select the path under the system variable, and then click Edit

Detailed explanation of object-oriented constructor in PHP# 6.

##          

7. Then fill in your php installation path and click OKDetailed explanation of object-oriented constructor in PHP

Note that the php installation path is the directory where you can see the php.exe file. Pay attention to the php version you choose. For example, my php here is version 7.0.12

Then fill in the following

D:\soft\develeopment\phpstudy\phpstudy2018\install\PHPTutorial\php\php-7.0. 12-NTS

Detailed explanation of object-oriented constructor in PHP 8. Finally, click to make sure

# 9. The directory where the php file is located Detailed explanation of object-oriented constructor in PHP

             

10. Then enter cmdDetailed explanation of object-oriented constructor in PHP

#11. In the open command line, enter php -f file name .php, the result will appear

##2. The most basic command in CmdDetailed explanation of object-oriented constructor in PHP

1. cls: clear command2. cd to enter a drive letter

3. dir displays all files in the directory

3. Garbled characters in the command line

The solution to the above Chinese garbled characters is as follows                  

Enter the command in the command line: chcp 65001 and it will switch to a new window. After typing, we will find that Chinese can be displayed normally

            2Detailed explanation of object-oriented constructor in PHP

Of course, switching to 65001 utf-8 encoding is only temporary. Next time you enter cmd, it will become the default encoding. If it appears again, just run it again as above.

4. How to check the current encoding in the command line ,\n
Difference

If the php file wants to display the newline effect in the command line, you need to write\n,
It can only be recognized in the browser as Line break. 11Detailed explanation of object-oriented constructor in PHP

Constructor function summary:

22Detailed explanation of object-oriented constructor in PHP

Definition

: It is a special function that is automatically executed when building an object. Specific implementation = public function __construct(){}

Function:

Initializing an object, in layman’s terms, means setting default attributes or default behaviors for the object

(Variable initialization means setting the initial value for the variable)

Features:

1. A function that will be automatically triggered when initializing the object. It will be the first function to be executed without manual call; 2 . The name cannot be customized and can only be named __construct;

3. If the function has parameters and you do not set a default value, you need to pass the parameters when using it, otherwise an error will be reported.

The above is the detailed content of Detailed explanation of object-oriented constructor in PHP. 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