Learning PHP while logging-(12) Object-Oriented Programming 2

Source: Internet
Author: User
: This article mainly introduces how to learn PHP-(12) Object-Oriented Programming 2 while logging. For more information about PHP tutorials, see. This article is separated from the previous article for a long time, and a small project is inserted in the middle. But it doesn't matter. "learn PHP while remembering" will continue.

PHP object-oriented programming

(2) Class attributes

The so-called class attribute is the variable declared in the class. The difference between it and the variables declared outside the class is that the modification permission is added before, that is, public/private/protected in the previous article. For example, I want to declare a student class that contains the student's student ID, name, gender, age, and class. I can declare it 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 above student class, I declare five attributes, all of which are private and private, so they cannot be directly accessed outside the class, therefore, I provide two methods for each of them to access them and set their values. Generally, when declaring a class, attributes are declared as private, and member methods are declared as public. then, you can access private attributes through public methods. GetXXX () and setXXX () are generally used when declaring access and setting. The first letter of XXX is generally capitalized. Of course, it can also be declared as a public attribute, so you can assign values to them outside the class and access them. However, it is recommended to declare a private attribute, because in the future, if the specification is met, each class will separate into a php file to store this class, if it is declared as private, the values in the objects of this class will not be changed at will, ensuring security.

When declaring an attribute, you can assign a value at the same time as an out-of-class variable.

Note:

Attribute can store a value, an array, or even an object of another class. For example, if you add a school attribute to a student, the school is also a class, including the school name and address. First, I declare the school class

class School{private $name;private $address;public getName(){return $this->name;}public getAddress(){return $this->address;}public setName($name){$this->name = $name;}public setAddress($address){$this->address = $address;}}
In this way, I can add school attributes to the student category:

 school;}...public setSchool($school){$this->school = $school;}}
In this way, it looks like other attributes, but when assigning parameters to them, you must note that the object is passed. For example:

I declare a school first:

$ School1 = new School (); $ school1-> setName ("Dalian University of Technology"); $ school1-> setAddress ("Dalian"); $ stu1 = new Student (); $ stu1-> setSchool ($ school1 );
In this way, the access to the school is an object. if I want to know the name of the school where the student is located, I will access it like this:

$stu1->getSchool()->getName();
If the getSchool method is used to obtain a School object, the getName method in the School object is used to access its name.

The $ this keyword is used to access the variable in the class, so let's talk about this keyword.

(3) $ this keyword

Whether it is declared as a public, private, or protected member variable, you must use the $ this keyword to access your own variables in the class. In my opinion, the $ this keyword refers to the class itself. I can also think of myself as an object to call my own attributes. Note that we use the $ symbol when declaring a variable. However, if $ this is used for access, only the "this" symbol is added with the "$" symbol, and the subsequent attributes are not added, if an error is added, an error is returned. Refer to the above code for specific usage. Of course, if the object declared outside is called the same as $ this, the object must be added with the $ symbol, which is not followed.

(4) static attributes

I didn't think of how to explain static attributes, but I saw this sentence in the book well: static attributes are commonly used to represent a continuous value related to a specific class and irrelevant to the instance object. We can regard static attributes as tired global variables. An important feature of static attributes is that you do not need to create a class instance when accessing static attributes, that is, you do not need to define class objects.

The book presents a static property of the sales volume in the car category. that is to say, no matter who buys the car, it is sold anyway, so I need to increase the sales volume by 1. However, I have already given examples of students and schools. I will continue with my example, saying that I think of this attribute of a school graduate, that is, no matter who you are, as long as I graduate from my school, the number of graduates will be 1 more. So I can declare:

public static $graduate;
That is, add the static keyword before the variable. So when I access this static property, I will not use the object to access it. I will use this class to access it directly. School: $ graduate =. This means that the objects that do not need to define classes can be accessed. Here I can use a picture to illustrate the difference between static attributes and common attributes:

Static attributes only occupy the memory. no matter the object instance does not exist, it will occupy its own memory unless the class is gone. Common attributes are allocated with memory each time an object is declared.

Static attributes are generally declared public.

(6) class constants

Like normal constants outside the class, class constants also store a fixed value. Use the const keyword for declaration. permission modification is not required here. When accessing a static attribute, you must use the class name: (double colon) and constant name. For example, if I declare a mobile phone type and the mobile phone has various models, I can store various mobile phone models with constants and assign values to these constants when assigning values to the mobile phone model attributes. But there may be such doubts, why should we use constants? I just declare it. It can be understood as follows:

Class Phone {const IPHONE = 1; const ZTE = 2; const HUAWEI = 3; // various properties}
I am a mobile phone class, which declares three constants. Note that the $ symbol is not required for declaring constants, and the constant name is generally capitalized. However, you should note that I store int-type data in it. if I assign a value to the mobile phone model, the value is similar to: $ this-> type = ZTE; however, if my model stores int data, it will occupy less memory. if I don't use the constant $ this-> type = 'zte'; then I will store the string. You can use constants to increase the compilation speed.

This is the advantage of constants I understand. But I have never used it.

Next, we will continue the object-oriented approach.

The above describes how to learn PHP-(12) Object-Oriented Programming 2, including some content, and hope to help those who are interested in PHP tutorials.

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.