##1, PHP object-oriented introduction
In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a whole composed of information and a description of how to process the information. It is an abstraction of the real world. In the real world, the things we face are objects, such as computers, televisions, bicycles, etc. The main three characteristics of the object: The behavior of the object: what operations can be applied to the object, turning on the light and turning off the light are behaviors. The shape of the object: how the object responds when those methods are applied, color, size, appearance. Representation of objects: The representation of objects is equivalent to an ID card, specifically distinguishing the differences in the same behavior and status. For example, Animal is an abstract class. We can specify a dog and a sheep, and dogs and sheep are concrete objects. They have color attributes, can be written, can run and other behavioral states. .Constructor − Mainly used to initialize the object when creating the object, that is, assign initial values to the object member variables. It is always used together with the new operator in the statement to create the object.
Destructor − Destructor (destructor) Contrary to the constructor, when the object ends its life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor. Destructors are often used to do "clean-up" work (for example, when creating an object, use new to open up a memory space, which should be released with delete in the destructor before exiting).
2. Define a student class
Object-oriented thinking puts forward two concepts, class and object. Class is a reference to a type of things. Abstract description, that is to say, the class needs to have the characteristics of this object. How do we usually describe the characteristics of a person?
For example, if we want to understand a student, we must first know at least the student's name and gender. , age, appearance, etc... We call these attributes in the class member attributes. After understanding a person's basic attributes, we also want to have a deeper understanding of what the person will do. This involves the person's behavior. It is called a member method in the class. In the method, we can define the specific role of this person. Take student as an example
First create a student class, defined as Student.class.php file (here for The difference between ordinary script files is that the class name is usually changed to the form of ".class.php", and the class name needs to be consistent with the file name)
Note: It is precisely because the file name of the class must be consistent with the name of the class Consistent, so a php file can only contain one class definition encoding.
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 9:42 */ //定义student类 class Student{ //成员属性 //成员方法 }
Usually PHP’s naming convention for classes is as follows:
(1). Use English nouns, and class files use Camel case notation. Separate words with uppercase letters, use lowercase letters for all other letters, use an uppercase letter for the first letter of a noun, and do not use underlines.
(2). Use complete words and avoid abbreviations (unless the abbreviation is more widely used, like URL, HTML)
(3). What you need to know first before naming a class what is it. If you still can't remember what the class is based on the clues provided by the class name, then your design is not good enough.
(4). Mixed class names consisting of more than three words can easily cause confusion among various entities in the system. Please redesign the class name.
For example:
class UserAccount{
...
}
class PaintingOrder{
.. .
}
3, Definition of member attributes and member methods
<?php class Student{ //成员属性 public $studentId; //学生学号 public $studentName;//学生姓名 public $studentAge; //学生年龄 //成员方法 public function goSchool(){ echo "{$this->studentName}去上学<br>"; } public function study($time){ echo "学习到{$time}"; } }
4, Creation of object
Create a new php file to instantiate the student object
object.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); echo "<pre>"; var_dump($student);//查看对象的类型以及具体数据 echo "</pre>";
The printing result is as follows:
5. Setting of member attributes
We can see that the attributes printed above are all null because no attributes have been set for it. Next We set the properties
The code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); $student->studentName='小张'; $student->studentId=1; $student->studentAge=25; echo "<pre>"; var_dump($student); echo "</pre>";
The printing result is as follows:
6, member method Call
Add the following code in object:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); $student->studentName='小张'; $student->studentId=1; $student->studentAge=25; $student->goSchool(); $student->study("22:00");
Print result: