Application of serialization polymorphism in PHP object-oriented automatic loading class object

Source: Internet
Author: User
Tags abstract class definition object serialization php file php and php error serialization

Auto Load Class
Many developers write object-oriented applications, creating a PHP source file for each class definition. A big annoyance is having to write a long list of included files at the beginning of each script (one file per class).

In a software development system, it is not possible to write all classes in a PHP file, and when you need to invoke a class declared in another file in a PHP file, you need to introduce the file through include. However, sometimes, in a large number of projects, to all the required class files included in, is a very frustrating thing, so we can not use what kind of time, and then this class in the PHP file to import it? That's what we're going to talk about here. Auto Load class.

In PHP 5, you can define a __autoload () function that is invoked automatically when you try to use a class that has not yet been defined, and by calling this function, the script engine has the last chance to load the required class before the PHP error fails, and the __autoload () function receives a parameter, is the class name of the class you want to load, so when you do the project, when you define the name of the class, you need to follow certain rules, preferably with the class name as the center, or you can add a uniform prefix or suffix to form the filename, such as xxx_classname.php, classname_ Xxx.php and just classname.php and so on.

This example attempts to load the MYCLASS1 and MYCLASS2 classes separately from the myclass1.php and myclass2.php files

The code is as follows Copy Code

function __autoload ($classname)
{
Require_once $classname. '. php ';
}

The MyClass1 class does not have an automatic call to the __autoload () function, passing in the argument "MyClass1"
$obj = new MyClass1 ();

The MyClass2 class does not have an automatic call to the __autoload () function, passing in the argument "MyClass2"
$obj 2 = new MyClass2 ();


Object serialization
Sometimes you need to transfer an object over the network, to facilitate transmission, you can convert the entire object into a binary string, and then revert to the original object when you reach the other end of the process, which is called serialization, as we now want to transport a car to the United States by ship, because the size of the car is relatively large, We could disassemble the car into smaller parts, and we would ship the parts to the United States by wheel, and then assemble the parts back into the United States.

There are two situations in which we have to serialize objects, the first of which is to serialize an object when it is transmitted over the network, and the second is to serialize the object to a file or database.

Serialization has two processes, one is serialized, is to convert objects into binary strings, we use the Serialize () function to serialize an object, the other is drag, is the conversion of objects into binary strings into objects, we use Unserialize () function to drag a row of an object.

PHP Serialize () function of the parameter is the object name, the return value is a string, Serialize () returned the string meaning is vague, generally we do not parse the string to get the object information, we just have to return the string to the other end of the network or save to the square.

PHP unserialize () function to drag the row object, the function of the parameter is the serialize () function of the return value, the output of course is the organization of the object.

The code is as follows Copy Code

Class Person
{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person

Define a constructor parameter to assign 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 speak the way to speak his own attributes
function Say ()
{
echo "My name is called:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. "";
}
}

$p 1=new person ("John", "Male", 20);

$p 1_string=serialize ($p 1); Serializing an object, returning a string

echo $p 1_string. ""; Serialized strings We don't usually parse them.

$p 2=unserialize ($p 1_string); Drag the serialization of a serialized string to form an object $p2

$p 2->say ();

Multi-state applications
polymorphism is one of the three characteristics of a surface object other than encapsulation and inheritance, I personally think that although PHP can achieve polymorphism, but and C + + Java and other object-oriented languages, polymorphism is not so prominent, because PHP itself is a weak type of language, There is no question of the conversion of a parent object into a subclass object or the conversion of a subclass object into a parent class object. So polymorphism is not so obvious; polymorphism refers to the ability of a program to handle multiple types of objects, such as working in a company, paying wages every month, and the same method of paying wages, Different employees or different positions in the company are issued through this method, but the wages are not the same. So there are many forms of the same wage-issuing method. For object-oriented programs, polymorphism is the target class object assignment to the parent class reference, and then call the method of the parent class to execute the subclass to overwrite the parent class of the method, but in PHP is weak type, object reference is the same without the parent class reference, or subclass reference.

Let's take a look at an example, first of all, to have the relationship between the parent class object and the subclass object by using polymorphism. Do a shape interface or an abstract class as a parent class, there are two abstract methods in it, one for the circumference, the other for the area; The subclass of this interface is a number of different shapes, each with a perimeter and an area, and because the parent class is an interface, So subclasses have to implement the parent of these two areas of the perimeter and the area of the abstract method, the purpose is to each of the different shapes of the subclass of the parent interface to comply with the specification, there must be a way to seek Zhou Changhe area.

The code is as follows Copy Code

Defines a shape's interface, which has two abstract methods for subclasses to implement
Interface Shape
{
function area ();
function perimeter ();
}

Defines a rectangular subclass that implements the perimeter and area of a 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 circumference of the rectangle is:". (2* ($this->width+ $this->height));
}
}

Defines a circular subclass that implements the perimeter and area of the shape interface
Class Circular implements Shape
{
Private $radius;

function __construct ($radius)
{
$this->radius= $radius;
}

function Area ()
{
Return "The area of the circle is:". (3.14* $this->radius* $this->radius);
}

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

A reference to a shape that is assigned to a rectangular object
$shape =new Rect (5, 10);

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

Butt Class A circular object is assigned to a reference to a shape
$shape =new Circular (10);

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

The previous example performs the result:
The size of the rectangle is: 50
The circumference of the rectangle is: 30
The area of the circle is: 314
Circumference of the circle is: 62.8

All right, about them. We have a detailed introduction, we do not know can also Baidu Ah.

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.