Home  >  Article  >  Backend Development  >  Learn PHP while memorizing-(12) Object-oriented programming 2

Learn PHP while memorizing-(12) Object-oriented programming 2

WBOY
WBOYOriginal
2016-08-08 09:32:30813browse

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;
	}

}

In the student class above, I declared five attributes, all of which are declared private. Then they cannot be accessed directly outside the class, so I Two methods are provided for each of their properties, to access them and to set their values. Generally, when declaring a class, the properties are declared as private and the member methods are declared as public, then the outside world can access the private properties through the public methods. And when declaring access and setting methods, the form getXXX() and setXXX() are generally used. The first letter of XXX is usually capitalized. Of course, they can also be declared as public properties, so they can be assigned values ​​directly and accessed outside the class. However, it is still recommended to declare it as a private attribute, because if it is standardized in the future, each class will live as a separate PHP file to specifically store this class. Declaring it as private will ensure that the values ​​​​in the objects of this class will not be changed at will, ensuring security.

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;
	}
}
Then I can add the school attribute to the student class like this:

<?php
class Student{
	...
	private $school;

	...
	public getSchool(){
		return $this->school;
	}

	...
	public setSchool($school){
		$this->school = $school;
	}

}
This way looks the same as other attributes, but when assigning it to When taking the parameter, please note that the object is passed. For example:

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);
In this way becomes an object when visiting its school. If I want to know the name of the school where this student is, I will To access it like this:

<span style="white-space:pre">	</span>$stu1->getSchool()->getName();
Use the getSchool method to get a School object, then use the getName method in the School object to access its name.

                                                                                                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;
That is, add the static keyword in front of the variable. Then when I access this static attribute, I don’t need to use an object to access it. I use this class to access it directly, School::$graduate = ...; and that’s it. This is what is said above that objects that do not need to be defined can be accessed. Here I can use a picture to illustrate the difference between static attributes and ordinary attributes:

静态属性只占这个内存,不管存不存在对象实例,除非这个类没了,否则会一直占有自己的内存。而普通的属性在每次声明对象的时候都会分配内存。

静态属性一般声明成公有的。

(6)类常量

与类外的普通常量一样,类常量也是存储一个固定的值。使用const关键字进行声明,这里不需要加权限修饰。在访问的时候跟访问静态属性一样需要使用类名::(双冒号)加常量名来访问。比如如果我声明一个手机类,手机有各种型号,那么我可以把各个手机型号用常量来存储,在给手机型号属性赋值的时候使用这些常量来赋值就好了。但是可能有这种疑惑,为什么要使用常量,我直接声明不就行了。那么可以这样理解:

class Phone{
	const IPHONE = 1;
	const ZTE = 2;
	const HUAWEI = 3;

    //这里面各种属性

}
我这个是一个手机类,里面声明了三个常量,这里注意,声明常量不需要$符号,并且常量的名字一般大写。但是你注意我在里面存储的是int型的数据,如果我给手机型号赋值的时候虽然给他赋值是类似:$this->type = ZTE;但是我的型号存的是int型的数据,那么就会占内存少,如果我不适用常量$this->type = 'ZTE';这样我就存的字符串。并且使用常量可以提高编译速度。

这是我理解的常量的优点。但是我没怎么用过。

下一篇继续面向对象。

以上就介绍了边记边学PHP-(十二)面向对象编程2,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn