In PHP object-oriented programming, You need to declare some methods that can operate on the attributes of the object's members to complete some object behavior. A function directly declared in a class is called a member method. The declaration of member methods is similar to that of function declaration, but some keywords can be added to control some permissions of member methods, such as public, protected, private, and static, without modifiers, public is used by default. Although public can be added or not, it is a good programming habit. The
Member attributes such as php Object-Oriented ProgrammingIn this article, we understand the definition of the member attribute of the class and the type and initialization of its variables. The member attribute is one of the members of the class. Next we will talk about the member method of another member of the class.
PHP Object-Oriented ProgrammingYou need to declare some methods that can operate on the attributes of the object member to complete some behavior of the object. A function directly declared in a class is called a member method. The declaration of member methods is similar to that of function declaration, but some keywords can be added to control some permissions of member methods, such as public, protected, private, and static, without modifiers, public is used by default. Although public can be added or not, it is a good programming habit. The member attributes and member methods of a class are optional. They can only have member attributes, member methods, or no member. The declaration of some member methods is as follows:
Class Person {function say () {// method this Person can talk echo "This Person is talking"; // method body} function eat ($ food) {// how this person can talk echo "This person is eating"; // method body}/* function fly () {// this person can fly echo "This person is flying ";}*/}
The eat method is declared with parameters. If no parameters are passed or the number of parameters is insufficient, for example:
$Person=new Person();echo $Person->eat();
The system reports an error.
In the Code shielding section, the method that a person can fly is declared. Obviously, a person cannot fly. This is not logical. Therefore, the declared member methods must be related to objects, it cannot be meaningless operations.