There is a bit of time between this article and the previous one, and a small project was inserted in the middle. But it doesn't matter, "Learning PHP while Memorizing" will continue.
PHP Object-Oriented Programming
(2) Class attributes
The so-called class attributes are variables declared in the class. The difference between it and variables declared outside the class is that the modified permissions are added in front, which is the public/private/protected in the previous article. For example, I want to declare a student class, which contains the student's student number, name, gender, age, class, etc. Then I can declare as follows:
<?php class Student{ private $sid; private $name; private $gender; private $age; private $grade; public getSid(){ return $this->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; } }
When declaring attributes, you can also assign values while declaring them, just like extra-class variables.
Note here:
Attributes can store a value, an array, or even an object of another class. For example, add school attributes to the student class above. This school is also a class, including the school's name, address, etc. I first 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; } }
<?php class Student{ ... private $school; ... public getSchool(){ return $this->school; } ... public setSchool($school){ $this->school = $school; } }
I first declare a school:
<span style="white-space:pre"> </span>$school1 = new School(); <span style="white-space:pre"> </span>$school1->setName("大连理工大学"); <span style="white-space:pre"> </span>$school1->setAddress("大连"); <span style="white-space:pre"> </span>$stu1 = new Student(); <span style="white-space:pre"> </span>$stu1->setSchool($school1);
<span style="white-space:pre"> </span>$stu1->getSchool()->getName();
used to use the $this keyword when accessing variables in the class, so let’s talk about the this keyword.
(3) $this keyword
Whether it is declared as a public, private or protected member variable, there will definitely be access to your own variables in the class, then you must use the $this key Character. In my opinion, the $this keyword refers to the class itself. I can also understand it as treating itself as an object to call its own properties. Note here that we use the $ symbol when declaring the variable, but if we use $this to access, only this plus the $ symbol, do not add the subsequent attributes, otherwise an error will be reported. For specific usage, refer to the code above. Of course, if the object declared externally is called, it is the same as $this, and the $ symbol must be added to the object, but not later.
(4) Static attributes
I didn’t think of how to explain static attributes, but I saw this sentence in the book that says it very well: Static attributes are often used to represent a specific A persistent value that depends on the class and not on the instance object. We can think of static properties as tired global variables. An important feature of static properties is that when accessing static properties, there is no need to create an instance of the class, that is, there is no need to define an object of the class.
In the book, there is a static attribute of the sales quantity in the car category. In other words, no matter who buys the car, if it is sold anyway, then my sales quantity will be increased by 1. But I have already given the example of students and schools above. I will continue my example and say that I think of this attribute of a school graduate. That is to say, no matter who you are, as long as you graduate from my school, then I There will be 1 more school graduates. Then I can declare it like this:
<span style="white-space:pre"> </span>public static $graduate;
静态属性只占这个内存,不管存不存在对象实例,除非这个类没了,否则会一直占有自己的内存。而普通的属性在每次声明对象的时候都会分配内存。
静态属性一般声明成公有的。
(6)类常量
与类外的普通常量一样,类常量也是存储一个固定的值。使用const关键字进行声明,这里不需要加权限修饰。在访问的时候跟访问静态属性一样需要使用类名::(双冒号)加常量名来访问。比如如果我声明一个手机类,手机有各种型号,那么我可以把各个手机型号用常量来存储,在给手机型号属性赋值的时候使用这些常量来赋值就好了。但是可能有这种疑惑,为什么要使用常量,我直接声明不就行了。那么可以这样理解:
class Phone{ const IPHONE = 1; const ZTE = 2; const HUAWEI = 3; //这里面各种属性 }
这是我理解的常量的优点。但是我没怎么用过。
下一篇继续面向对象。
以上就介绍了边记边学PHP-(十二)面向对象编程2,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。