PHP Object-Oriented: Member method, member variable, class constant

Source: Internet
Author: User
member Methods

Before we talked about the definition and instantiation of the PHP class, for example, we created a class for a person.

But people not only have attributes, but also behavior, such as people can run, can Dance, can sing, can eat and so on. So, how do we go about implementing these behaviors? The next step is to use our member approach to achieve it.

Or an example of the last lesson, defining a person's class, creating a running member method

Class preson{public     $name;     public $age;     public $gender;     Public Function Run () {           //Declare member Method                   echo "Man in Tower";              }} First instantiate a class $preson1 = new Preson (), $Preson 1->name = "Demacia";//Call member Method $preson1->run ();

This example is a simple member method of creating a call.

Member variables

A variable in a class, also called a member variable, has been used in our previous example and is presented here to illustrate.

Class preson{public   $name;              Define member variables public   $age;}

The $name in the example above is the member variable.

Class constants

There are variables in the class, then there will be constants relative to them. Constant means that the quantity that does not change is a constant value.

Define constants, we use Const.

<?phpclass  character{public $name;                     Declare a variable const SKILLS = ' grams ';  //Declare a constant} $character 1 = new character (); $character 1->name = "ya"; Echo ' I'm going to play '. $character 1->name. ' <br/> '. ' Skill is '. Character::skills;

The above example shows how constants are defined and how they are accessed.

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.