Experience clas...LOGIN

Experience classes and objects



##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. .


Object-oriented content

Class − defines the abstract characteristics of a thing. The definition of a class includes the form of the data and the operations on the data.

Object − is an instance of a class.

Member variables − Variables defined inside the class. The value of this variable is invisible to the outside world, but can be accessed through member functions. After the class is instantiated as an object, the variable can be called an attribute of the object.

Member function − Defined inside the class, it can be used to access the data of the object.

Inheritance − Inheritance is a mechanism for subclasses to automatically share data structures and methods of parent classes. This is a relationship between classes. When defining and implementing a class, you can do it on the basis of an existing class, take the content defined by the existing class as your own content, and add some new content.

Parent class − A class is inherited by other classes. This class can be called a parent class, a base class, or a super class.

Subclass − A class that inherits other classes is called a subclass, or it can also be called a derived class.

Polymorphism − Polymorphism means that the same function or method can act on multiple types of objects and obtain different results. Different objects can produce different results when receiving the same message. This phenomenon is called polymorphism.

Overloading − Simply put, it is a situation where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or methods. .

Abstraction − Abstraction refers to abstracting objects with consistent data structures (attributes) and behaviors (operations) into classes. A class is an abstraction that reflects important properties related to an application while ignoring other irrelevant content. The division of any class is subjective, but must be related to the specific application.

Encapsulation − Encapsulation refers to binding the properties and behavior of an object that exists in the real world together and placing it in a logical unit.

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{
    //成员属性
  
    //成员方法
    
}

微信图片_20180303100912.png

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:

微信图片_20180303102957.png

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:

微信图片_20180303103602.png

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:

微信图片_20180303104458.png





#Next Section

<?php echo "类与对象的使用"; ?>
submitReset Code
ChapterCourseware