Php object-oriented: Member methods, member variables, class constants

Source: Internet
Author: User
Previously we talked about classes and objects and instantiated classes. for example, we created a person's class. However, people are not just attributes, but also behavior patterns, such as running, dancing, singing, and eating. So how can we implement these behaviors? Next, we will use our member method to implement it. The example of the previous lesson defines a person's class, Member method

Previously, we talked about the definition and instantiation of php classes. for example, we created a person's class.

However, people are not just attributes, but also behavior patterns, such as running, dancing, singing, and eating. So how can we implement these behaviors? Next, we will use our member method to implement it.

The example of the previous lesson defines a person's class and creates a running Member method.

Class Preson {public $ name; public $ age; public $ gender; public function Run () {// declare the member method echo "";}} // instantiate a class $ Preson1 = new Preson (); $ Preson1-> name = "demassiya"; // call the member method $ Preson1-> Run ();

This example is a simple call to create a member method.

Member variables

The variables in the class, also known as member variables, have been used in our previous examples. here we will introduce them to you.

Class Preson {public $ name; // defines the member variable public $ age ;}

In the above example, $ name is a member variable.

Class constant

If there are variables in the class, there will be constants. A constant is a constant value that does not change.

Define constants. We use const.

 Name = "Aesop"; echo 'I want to play'. $ character1-> name .'
'.' Skill is '. character: SKILLS;

The above example illustrates the definition and access methods of constants.

The above is php object-oriented: detailed content of member methods, member variables, and class constants. For more information, see other related articles in the first PHP community!

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.