Home > Backend Development > PHP Problem > What are php member methods

What are php member methods

藏色散人
Release: 2023-03-05 12:18:02
Original
2002 people have browsed it

php member method is also called member function, which is defined inside the class and can be used to access the data of the object; the syntax format of php member method is [[modifier] function method name (parameter..){[ Method body][return return value]}].

What are php member methods

Recommended: "PHP Video Tutorial"

Member properties and member methods in PHP classes

类的声明
成员属性
成员方法(成员函数 − 定义在类的内部,可用于访问对象的数据)
Copy after login

Class declaration

Simple format:

[修饰符] class 类名{ //使用class关键字加空格后加上类名
[成员属性] //也叫成员变量
[成员方法] //也叫成员函数
}
Copy after login

Full format:

[修饰符] class 类名 [extends 父类] [implements 接口1[,接口2...]]{
[成员属性] //也叫成员变量
[成员方法] //也叫成员函数
}
Copy after login

Member attributes

Format:

Modifier $variable name[=default value]; //For example: public $name="zhangsan";

Note: Member attributes cannot be expressions, variables, or methods with operators or function call.

    public $var3 = 1+2;          //错误格式
    public $var4 = self::myStaticMethod();   //错误格式
    public $var5 = $myVar;           //错误格式
Copy after login

Correct definition:

    public $var6 = 100; //普通数值(4个标量:整数、浮点数、布尔、字串)
    public $var6 = myConstant;       //常量
    public $var7 = self::classConstant;     //静态属性
    public $var8 = array(true, false);      //数组
Copy after login

Common attribute modifiers: public, protected, private, static, var (obsolete)

Member method

Member Method format:

[修饰符] function 方法名(参数..){
[方法体]
[return 返回值]
}
Copy after login

Modifiers: public, protected, private, static, abstract, final

The declared member method must be related to the object and cannot be some meaningless operation

      //下面声明了几个人的成员方法,通常将成员方法声明在成员属性的下面
       public function say(){    //人可以说话的方法
            echo "人在说话";        //方法体
        }       
       public function run(){     //人可以走路的方法
            echo "人在走路";        //方法体
        }
<?php   //声明一个电话类,类名为Phone
    class Phone {                   
        //声明4个与电话有关的成员属性
        public $Manufacturers;        //第一个成员属性,用于存储电话的外观
        public $color;                       //第二个成员属性,用来设置电话的外观颜色
        public $Battery_capacity;    //第三个成员属性,用来定义电话的电池容量
        public $screen_size;           //第四个成员属性,用来定义电话的屏幕尺寸
        //第一个成员方法用来声明电话具有接打电话的功能
        public function call(){                
            echo "正在打电话";         //方法体,可以是打电话的具体内容
        }
        //第二个成员方法用来声明电话具有发信息的功能
        public function message(){             
            echo "正在发信息";         //方法体,可以是发送的具体信息
        }
        //第三个成员方法用来声明电话具有拍照的功能
        public function photo() {               
             echo "正在拍照";           //方法体,可以是拍照的整个过程
        }
    }
Copy after login

The above is the detailed content of What are php member methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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