Class and object learning notes in php

Source: Internet
Author: User
This article will introduce you to some usage details of php object-oriented methods. here we will mainly talk about classes and object learning notes in php, and hope to help you. Object-Oriented Thinking

This article will introduce you to some usage details of php object-oriented methods. here we will mainly talk about classes and object learning notes in php, and hope to help you.

Object-Oriented Thinking

Object-Oriented Programming (OOP) is a type of Programming model, and colleagues are also a method of Programming. It uses 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, process-oriented and object-oriented are similar to imperative programming). They are three different coding and design styles. The core idea of object-oriented is Objects, encapsulation, reusability and scalability.

Object-oriented is a more advanced and abstract way of thinking. although process-oriented is also an abstraction, process-oriented is a basic abstraction, object-oriented is a high-level abstraction built on process-oriented and above. Therefore, object-oriented understanding is not that easy.

Class is the 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, containing the definition of class members and methods, as shown in the following code:

  1. Class person {
  2. Public $ name;
  3. Public $ gender;
  4. Public function say (){
  5. Echo $ this-> name. "is". $ this-> gender;
  6. }
  7. }

The following code generates an instance of this class:

  1. $ Student = new person ();
  2. $ Student-> name = "Tome ";
  3. $ Student-> gender = "male ";
  4. $ Student-> say ();
  5. $ Teacher = new person ();
  6. $ Teacher-> name = "kati ";
  7. $ Teacher-> gender = "female ";
  8. $ Teacher-> say ();

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

Some understandings of classes and objects:

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 specific value of the class property, which is the class instantiation. It is precisely because of different attributes that different objects can be distinguished. In the preceding example, student and teacher are separated by gender and name.

The relationship between classes and objects is similar to the relationship between services, processing, and processing. Specifically, it is like the relationship between raw materials and pipelines, you only need to call methods in the class on the object to process the class attributes and display their functions.

Print the student object. the code is as follows:

  1. Print_r (array) $ student );
  2. Var_dump ($ student );

Serialization object. the instance code is as follows:

  1. $ Str = serialize ($ student );
  2. Echo $ str;
  3. File_put_contents('store.txt ', $ str );
  4. // Output result: 0: 6: "person": 2: {s: 4: "name"; s: 3: "Tom"; s: 6: "gender "; s: 4: "mail ";}

Deserialization object. the instance code is as follows:

  1. $ Str = file_get_contents('store.txt ');
  2. $ Student = unserialize ($ str );
  3. $ 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.