Learn php-(12) object-oriented Programming 2

Source: Internet
Author: User
this one is a little bit longer than the previous one, and a small project is inserted in the middle. But it doesn't matter, "learn PHP while learning" will continue to go on.

PHP Object-Oriented programming

(2) Class properties

The so-called Class attribute is the variable declared inside the class. It differs from the variable declared outside the class by the addition of the modifier permission, which is the public/private/protected in the previous article. For example, I want to declare a student class, which contains the student's school number, name, gender, age, class and so on. Then I can declare as follows:

 
  Sid;} Public GetName () {return $this->name;} Public Getgender () {return $this->gender;} Public Getage () {return $this->age;} Public Getgrade () {return $this->grade;} Public Setsid ($SID) {$this->sid = $sid;} Public SetName ($name) {$this->name = $name;} Public Setgender ($gender) {$this->gender = $gender;} Public Setage ($age) {$this->age = $age;} Public Setgrade ($grade) {$this->grade = $grade;}}

in the student class above, I declare five attributes, all declared as private, so the outside of the class is not directly accessible to them, so I give them each of the properties of two methods, respectively, to access them and set their values. In general, when declaring a class, the property is declared private, and the member method is declared public, and the private property can be accessed by the public method outside. And in the Declaration of Access and set two methods generally use GETXXX () and setxxx () This form, xxx The first letter is generally uppercase. Of course, they can be declared as public properties, so they can be assigned directly outside the class and accessed. However, it is recommended to declare a private property, because if the specification later, each class will be a separate life into a PHP file, specifically to store the class, declared as private will ensure that the value of the object in this class is not arbitrarily changed, security is guaranteed.

When declaring a property, you can also assign a value, just like an out-of-class variable.

Note here:

A property can store a value, an array, or even an object of another class. For example, the students in the above class to add to his school attributes, the school is also a class, including the school's name, address and so on. I first declare the school class

Class School{private $name;p rivate $address;p ublic getName () {return $this->name;} Public getaddress () {return $this->address;} Public SetName ($name) {$this->name = $name;} Public Setaddress ($address) {$this->address = $address;}}
so I can do that in a student class. Attribute of the school:

 
  School;} ... public setschool ($school) {$this->school = $school;}}
this It looks like other properties, but when assigning a parameter to him, notice that the object is passed. For example:

I declare a school first:

$school 1 = new School ();  $school 1->setname ("University of Dalian");  $school 1->setaddress ("Dalian");  $stu 1 = new Student ();  $stu 1->setschool ($school 1);
this It was an object when I visited its school, and if I wanted to know the name of the school where the student was, I would visit:

$stu 1->getschool ()->getname ();
using the Getschool method to get a school object, use the GetName method in the school object to access its name.

Here, when accessing variables within a class, use the $this keyword, and then say the This keyword.

(3) $this keywords

Whether you declare a member variable as public or private or protected, you must have access to your own variable in the class, and you will need to use the $this keyword. $this keyword in my opinion refers to the class itself, I can also understand that I think of myself as an object to invoke their own properties. Note here that we used the $ symbol when declaring the variable, but if you use $this access, only this plus the $ symbol, the following attribute will not be added, if added will be an error. In particular, refer to the code above. Of course, if the object is declared outside the same time as $this, the object to add the $ symbol, followed by no Add.

(4) Static properties

For static properties I didn't think how to explain it, but I saw it in the book. A static property is commonly used to represent a continuous value that is related to a particular class and not to an instance object. We can think of static properties as a global variable that is exhausted. An important feature of static properties is that there is no need to create an instance of the class when accessing a static property, that is, an object that does not need to define the class.

In the book to give a car in the number of sales of static properties, that is, whoever bought the car, anyway, is sold, then my sales volume will add 1. But I've already given examples of students and schools, and I'm going to follow my example and say, I think of this attribute of a school graduate, which means that whoever you are, as long as you graduate from my school, my school graduates will be 1 more. Then I can declare:

public static $graduate;
This is the keyword that adds static to the front of the variable. So when I visit this static property, I do not use the object to access, directly use this class to access, School:: $graduate = ...; You can do it. This is what the above said does not need to define the class object can be accessed. Here I can use Zhang Tulai to illustrate the difference between a static property and a normal property:

Static properties occupy only this memory, regardless of the existence of the object instance, unless the class is gone, it will always occupy its own memory. The normal property allocates memory each time the object is declared.

Static properties are generally declared as public.

(6) Class constants

Like ordinary constants outside the class, class constants store a fixed value as well. Declare with the const keyword, which does not require a permission modifier. When accessing a static property, you need to use the class name:: (double colon) plus the constant name to access it. For example, if I declare a mobile phone, the phone has a variety of models, then I can put the individual phone models with constants to store, in the mobile phone model attributes to assign value when the use of these constants to assign value. But there may be such doubts, why use constants, I directly declare not on the line. Then you can understand this:

Class Phone{const IPHONE = 1;const ZTE = 2;const HUAWEI = 3;    The various properties within this}
My This is a mobile phone class, which declares three constants, here note that the declaration of a constant does not require the $ symbol, and the name of the constant is generally uppercase. But you notice what I'm storing in it is the int type data, and if I assign a value to the phone model, it is similar: $this->type = ZTE; But my model has data of type int, then it will take up less memory if I don't apply constant $this-> Type = ' ZTE '; so I'll save the string. and using constants can improve compilation speed.

This is the advantage of the constant I understand. But I didn't use it much.

The next article continues to object-oriented.

The above describes the edge of learning php-(12) object-oriented programming 2, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.