Php object-oriented automatic loading of serializing multi-State applications of class objects

Source: Internet
Author: User
Tags autoload object serialization php error

This article describes how to automatically load an application instance of serialized multi-state application of class objects in php. If you need to know it, you can take a look.

Automatic loading class
Many developers create a PHP source file for each class definition when writing object-oriented applications. A big headache is that you have to write a long list of contained files at the beginning of each script (each class has one file.

In a software development system, it is impossible to write all classes in one PHP file. When one PHP file needs to call the classes declared in another file, you need to introduce this file through include. However, when there are many file projects, it is a headache to include all the files of the required classes one by one, so can we use any classes, what about importing the PHP file where this class is located? This is the automatic loading class we will talk about here.

In PHP 5, A _ autoload () function can be defined, which is automatically called when trying to use a class that has not been defined. by calling this function, before a PHP error fails, the script engine has the last chance to load the required class. A parameter received by the _ autoload () function is the class name of the class you want to load, therefore, when you create a project, you need to follow certain rules when organizing the file name of the class, it is best to center the class name, or you can add a uniform prefix or suffix to form a file name, for example, xxx_classname.php, classname_xxx.php, and classname. php, etc.

In this example, we try to load the MyClass1 and MyClass2 classes in the MyClass1.php and MyClass2.php files respectively.

The Code is as follows: Copy code

Function _ autoload ($ classname)
{
Require_once $ classname. '. php ';
}

// The MyClass1 class does not automatically call the _ autoload () function. The input parameter "MyClass1"
$ Obj = new MyClass1 ();

// The MyClass2 class does not automatically call the _ autoload () function. The input parameter "MyClass2"
$ Obj2 = new MyClass2 ();


Object serialization
Sometimes an object needs to be transmitted over the network. To facilitate transmission, the entire object can be converted into a binary string and restored to the original object when it reaches the other end, this process is called serialization, just as we want to ship a car to the United States through a ship. because the size of the car is large, we can split the car into small parts, then we ship these parts to the United States in a round, and then assemble them into the United States and return them to the automobile.

There are two cases where we must serialize objects. The first case is to serialize objects when transmitting an object over the network, the second case is to use serialization when writing objects into a file or database.

There are two steps of serialization. One is serialization, that is, converting an object into a binary string. We use the serialize () function to serialize an object, and the other is deserialization, it is to convert the binary string converted by the object into an object. We use the unserialize () function to deserialize an object.

The parameter of the serialize () function in PHP is the object name, and the returned value is a string. The meaning of the string returned by Serialize () is fuzzy. Generally, this string is not parsed to obtain the object information, we only need to upload the returned string to the other end of the network or save it to the component.

In PHP, The unserialize () function is used to deserialize objects. The parameter of this function is the return value of the serialize () function, and the output is of course a re-organized object.

The Code is as follows: Copy code

Class Person
{
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person

// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = "", $ sex = "", $ age = "")
{
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}

// This person can talk about his/her attributes.
Function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "";
}
}

$ P1 = new Person ("Zhang San", "male", 20 );

$ P1_string = serialize ($ p1); // serializes an object and returns a string

Echo $ p1_string. ""; // serialized strings are not parsed.

$ P2 = unserialize ($ p1_string); // concatenates A serialized string to form an object. $ p2

$ P2-> say ();

Application with Polymorphism
Polymorphism is one of the three major features of another surface object except encapsulation and inheritance. In my opinion, although polymorphism can be achieved in PHP, but compared with the object-oriented languages c ++ and Java, polymorphism is not so prominent, because PHP itself is a weak language, there is no problem in converting a parent class object to a subclass object or a subclass object to a parent class object, so the application of polymorphism is not so obvious; polymorphism refers to the ability of a program to process multiple types of objects, such as working at a company, paying a monthly financial account, and sending a salary in the same way, different employees in the company or employees in different positions are issued through this method, but the salaries are different. Therefore, there are many forms of the same wage payment method. For Object-oriented Programs, polymorphism is to assign a subclass object to the parent class for reference, and then call the parent class method to execute the method that the subclass overrides the parent class, however, PHP is of a weak type, and object references are the same regardless of the parent class or subclass references.

Now let's look at an example. To use polymorphism, there must be a relationship between the parent class object and the subclass object. Create a shape interface or abstract class as the parent class. There are two abstract methods, one for perimeter and the other for area. The sub-classes of this interface are different shapes, each shape has a length and area, and because the parent class is an interface, the Child class must implement the abstract methods of the two weeks and area of the parent class, in this way, child classes of different shapes comply with the parent class interface specifications, and there must be a method for calculating the circumference and area.

The Code is as follows: Copy code

// Defines a shape interface, which has two abstract methods for sub-classes to implement
Interface Shape
{
Function area ();
Function perimeter ();
}

// Defines a rectangular subclass to implement the circumference and area of the Shape interface.
Class Rect implements Shape
{
Private $ width;
Private $ height;

Function _ construct ($ width, $ height)
{
$ This-> width = $ width;
$ This-> height = $ height;
}

Function area ()
{
Return "The area of the rectangle is:". ($ this-> width * $ this-> height );
}

Function perimeter ()
{
Return "the perimeter of the rectangle is:". (2 * ($ this-> width + $ this-> height ));
}
}

// Defines a circular subclass to implement the circumference and area of the Shape interface.
Class Circular implements Shape
{
Private $ radius;

Function _ construct ($ radius)
{
$ This-> radius = $ radius;
}

Function area ()
{
Return "the circular area is:". (3.14 * $ this-> radius );
}

Function perimeter ()
{
Return "the circumference of the circle is:". (2*3.14 * $ this-> radius );
}
}

// Assign the subclass rectangle object to a reference of the shape
$ Shape = new Rect (5, 10 );

Echo $ shape-> area ()."
";
Echo $ shape-> perimeter ()."
";

// Assign the circular object of the subclass to a reference of the shape.
$ Shape = new Circular (10 );

Echo $ shape-> area ()."
";
Echo $ shape-> perimeter ()."
";

Result of the preceding example:
The area of the rectangle is: 50.
The circumference of the rectangle is: 30.
Circular area: 314
The circle circumference is: 62.8

Now we have a detailed introduction to the three of them. If you don't understand them, you can use Baidu.


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.