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]}].
Recommended: "PHP Video Tutorial"
Member properties and member methods in PHP classes
类的声明 成员属性 成员方法(成员函数 − 定义在类的内部,可用于访问对象的数据)
Class declaration
Simple format:
[修饰符] class 类名{ //使用class关键字加空格后加上类名 [成员属性] //也叫成员变量 [成员方法] //也叫成员函数 }
Full format:
[修饰符] class 类名 [extends 父类] [implements 接口1[,接口2...]]{ [成员属性] //也叫成员变量 [成员方法] //也叫成员函数 }
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; //错误格式
Correct definition:
public $var6 = 100; //普通数值(4个标量:整数、浮点数、布尔、字串) public $var6 = myConstant; //常量 public $var7 = self::classConstant; //静态属性 public $var8 = array(true, false); //数组
Common attribute modifiers: public, protected, private, static, var (obsolete)
Member method
Member Method format:
[修饰符] function 方法名(参数..){ [方法体] [return 返回值] }
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 "正在拍照"; //方法体,可以是拍照的整个过程 } }
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!