Php object-oriented programming and other member methods-PHP development

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.