search
HomeBackend DevelopmentPHP ProblemWhat are php member methods

What are php member methods

Aug 29, 2020 am 09:15 AM
php

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

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

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version