在php面向对象编程之类的成员属性这一篇,我们知道了类的成员属性的定义及其变量的类型和初始化,成员属性是类的成员之一,接下来我们讲类的另一个成员--成员方法。
PHP面向对象编程中,需要声明一些可以操作本对象成员属性的一些方法,来完成对象的一些行为。在类中直接声明的函数就称为成员方法。成员方法的声明和函数声明差不多,只不过可以加一些关键字来修饰,用来控制成员方法的一些权限,如public、protected、private和static等,不加修饰符的情况下默认是使用public来修饰,虽然public可加可不加,但是加上是一种良好的编程习惯。类的成员属性和成员方法都是可选的,可以只有成员属性,也可以只有成员方法,当然也可以没有任何成员。下面是一些成员方法的声明:
class Person{ function say(){ //这个人可以说话的方法 echo "这个人在说话"; //方法体 } function eat($food){ //这个人可以说话的方法 echo "这个人在吃东西"; //方法体 } /* function fly(){//这个人可以飞的方法 echo "这个人在飞"; } */ }
上面的eat方法声明时带有参数,如果调用这个方法时没有传递参数,或者参数数量不足,如:
$Person=new Person(); echo $Person->eat();
系统会报出错误。
在代码的屏蔽部分声明了一个人会飞的方法,显然,人是不会飞的,这样不符合逻辑的,所以声明的成员方法必须和对象相关,不能是一些没有意义的操作。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.