Class, Object learning notes in PHP

Source: Internet
Author: User

Object oriented thought

Object-Oriented programming (Object-oriented Programming,oop) is a programming paradigm, and colleagues are also a method of program development. It encapsulates the program and data as the basic unit of the program to improve the reusability, flexibility and scalability of the software.

Process-oriented, object-oriented, and functional programming are known as the three Paradigms of programming languages (in fact, both process-oriented and object-oriented programming are both imperative), three different coding and design styles. The core idea of object-oriented is object, encapsulation, reusability and scalability.

Object-oriented is a more advanced, more abstract way of thinking, although the process is an abstraction, but process-oriented is a basic abstraction, object-oriented is based on process-oriented higher level of abstraction, so object-oriented understanding is not so easy.

Class is a description of a group of objects in our team

In PHP, the definition of each class begins 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. 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;
}
}

You can then 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 ();
$teacher->name= "Kati";
$teacher->gender= "female";
$teacher->say ();

This code instantiates the person class, producing an instance of the student object and the teacher object. In fact, it is from abstraction to concrete process.

Some understanding of classes and objects:

class defines a series of properties and methods, and provides practical operational details that can be used to process properties.
object contains the specific value of the class attribute, which is the instantiation of the class. It is precisely because of the different properties that the different objects can be distinguished. In the above example, because student and teacher's sex and the name is different, only then can divide the two people in the district.
The relationship between a class and an object is similar to the relationship between being serviced, machined, and processed, and in particular, the relationship between raw materials and pipelining. You can process the properties of a class and display its functionality by simply invoking the methods that exist in the class on the object.
Print Student Objects

The code is as follows Copy Code

Print_r ((array) $student);
Var_dump ($student);

Serializing an Object

The code is as follows Copy Code

$str = serialize ($student);
Echo $str;
File_put_contents (' Store.txt ', $str);

Output results:

0:6: "Person": 2:{s:4: "Name"; S:3: "Tom"; s:6: "Gender"; s:4: "Mail";}

Deserializing objects

  code is as follows copy code

$str = File_ Get_contents (' store.txt ');
$student = unserialize ($STR);
$student->say ();

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.