What is the meaning of member method in php

WBOY
Release: 2023-03-15 14:26:02
Original
1856 people have browsed it

In PHP, member methods are member functions, which are used to access the data of an object. They are defined inside the class. The syntax is "function method name (parameters..){[method body][ return return value]}".

What is the meaning of member method in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the meaning of member method in php

Member method (member function − is defined inside the class and can be used to access the data of the object)

Simple format:

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

Full format:

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

Member attribute

Format:

修饰符 $变量名[=默认值];
 //如:public $name="zhangsan";
Copy after login

Note: Member attributes cannot be expressions, variables, methods 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

Examples are as follows:

<?php
    class ren{        //定义人类
        private function dance(){        //定义private成员方法dance
            echo &#39;我要跳一支舞。&#39;;
        }
        private function sing(){        //定义private成员方法sing
            echo &#39;我要唱一首歌。&#39;;
        }
        public function do_something($item){        //定义public成员方法do_something
            switch($item){
                case &#39;dance&#39;:
                    $this->dance();        //调用类成员方法dance
                    break;
                case &#39;sing&#39;:
                    $this->sing();        //调用类成员方法sing
                    break;
            }
        }
    }
    $ren=new ren();        //实例化人类的对象
    //访问类成员方法并传入不同的参数
    $ren->do_something(&#39;sing&#39;);
    $ren->do_something(&#39;dance&#39;);
?>
Copy after login

Recommended study: " PHP video tutorial

The above is the detailed content of What is the meaning of member method in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!