This article will introduce to you some detailed usage of object-oriented in PHP. Here I mainly talk about the class and object study notes in PHP. I hope it will be helpful to all students.
Object-oriented thinking
Object-Oriented Programming (OOP) is a programming paradigm and also a program development method. It takes objects as the basic unit of a program and encapsulates programs and data to improve software reusability, flexibility and scalability.
Process-oriented, object-oriented and functional programming are known as the three major paradigms in programming languages (in fact, both process-oriented and object-oriented belong to imperative programming), which are three different coding and design styles. The core ideas of object-oriented are objects, encapsulation, reusability and scalability.
Object-oriented is a more advanced and abstract way of thinking. Although process-oriented is also a kind of abstraction, process-oriented is a basic abstraction. Object-oriented is a higher-level abstraction based on process-oriented. , so object-oriented understanding is not so easy.
A class is a description of a group of objects in our team
In PHP, the definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly braces, which contains the definition of class members and methods. As shown in the following code
The code is as follows |
Copy code |
代码如下 |
复制代码 |
class person{
public $name;
public $gender;
public function say(){
echo $this->name."is ".$this->gender;
}
}
|
class person{
Public $name;
Public $gender;
代码如下 |
复制代码 |
$student = new person();
$student->name="Tome";
$student->gender= "male";
$student->say();
$teacher= new person();
$teacher->name="kati";
$teacher->gender= "female";
$teacher->say();
|
Public function say(){
echo $this->name."is ".$this->gender;
}
}
|
Next, you can generate an instance of this class:
The code is as follows |
Copy code |
$student = new person();
$student->name="Tome";
$student->gender= "male";
$student->say();
$teacher= new person();
代码如下 |
复制代码 |
print_r((array)$student);
var_dump($student);
|
$teacher->name="kati";
$teacher->gender= "female";
$teacher->say();
代码如下 |
复制代码 |
$str = serialize($student);
echo $str;
file_put_contents('store.txt',$str);
输出结果:
0:6:"person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"mail";}
|
|
This code instantiates the person class and generates an instance of a student object and a teacher object. In fact, it is the process from abstract to concrete.
Some understanding of classes and objects:
The class defines a series of attributes and methods, and provides actual operation details. These methods can be used to process attributes.
The object contains the concrete values of the class attributes, which is the instantiation of the class. It is precisely because of the difference in attributes that different objects can be distinguished. In the above example, the student and teacher can be distinguished because their genders and names are different.
The relationship between classes and objects is similar to the relationship between serving, processing and being processed. Specifically, it is like the relationship between raw materials and assembly lines. You only need to call the methods existing in the class on the object to process the attributes of the class and display its functions.
Print student object
The code is as follows |
Copy code |
print_r((array)$student);
var_dump($student);
|
Serialized object
The code is as follows |
Copy code |
$str = serialize($student);
echo $str;
file_put_contents('store.txt',$str);
Output result:
0:6:"person":2:{s:4:"name";s:3:"Tom";s:6:"gender";s:4:"mail";}
|
Deserialized object
The code is as follows
代码如下 |
复制代码 |
$str = file_get_contents('store.txt');
$student = unserialize($str);
$student->say();
|
|
Copy code |
|
$str = file_get_contents('store.txt');
$student = unserialize($str);
$student->say();
http://www.bkjia.com/PHPjc/632678.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632678.htmlThis article will introduce to you some detailed usage of object-oriented in php. Here we mainly talk about classes in php , object study notes, I hope it will be helpful to all students. Object-oriented thinking...