PHP Object-oriented automatic loading class object serialization polymorphism Application _php Tutorial

Source: Internet
Author: User
Tags object serialization
This article introduces the application examples of the application of the automatic loading class object serialization polymorphism in the object oriented in PHP, and the students who need to know can look at it.

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 the 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 each of the required classes of files are included in, is a very annoying thing, so we can use what class, and then the class where the PHP file 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 will be called automatically when trying to use a class that has not yet been defined, and by calling this function, the scripting engine has a last chance to load the required class before PHP fails, and a parameter that the __autoload () function receives. is the class name of the class you want to load, so when you do the project, when the organization defines the name of the class, you need to follow certain rules, preferably with the class name as the center, or a uniform prefix or suffix to form the file name, such as xxx_classname.php, classname_ Xxx.php and is classname.php and so on.

This example attempts to load the MyClass1 and MyClass2 classes from 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 have an automatic call to the __autoload () function, passing in the parameter "MyClass1"
$obj = new MyClass1 ();

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


Serialization of objects
Sometimes you need to transfer an object on the network, in order to facilitate the transfer, the whole object can be converted into a binary string, until the other end, and then revert to the original object, this process is called serialization, as we now want to transport a car through the ship to the United States, because the car volume is relatively large, We can take the car apart into small parts, and we'll ship the parts to the United States by wheel, and then assemble them back into the United States.

There are two cases where we have to serialize objects, the first is to serialize an object when it is being transmitted over the network, and the second is to serialize the object when it is written to a file or database.

Serialization has two processes, one is serialization, is to convert the object into a binary string, we use the Serialize () function to serialize an object, the other is crossdress, that is, the conversion of the object binary string into an object, we use the unserialize () function to crossdress an object.

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

The Unserialize () function in PHP crossdress the line object, which is the return value of the Serialize () function, and the output is, of course, a re-organized object.

code as follows copy code

class person
{
//The following is a member of the human attribute
var $name; The name of the person
Var $sex;//person's gender
Var $age;//person's age

//Define a constructor method parameter to assign a property 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 in a way that speaks his own properties
function say ()
{
echo "My name is called". $this->name. "Gender:". $this->sex. " My age is: ". $this->age.";
}
}

$p 1=new person ("Zhang San", "male", +);

$p 1_string=serialize ($p 1);//Serialize an object to return a string

Echo $p 1_string. "";//serialization of strings we don't usually parse

$p 2=unserialize ($p 1_string);//Serialize a serialized string crossdress to form an object $P2

$p 2->say ();

Application of polymorphism
Polymorphism is one of the three features of a polygon object other than encapsulation and inheritance, and I personally think that although it can be polymorphic in PHP, polymorphism is not as prominent as C + + and Java's object-oriented language, because PHP itself is a weak type of language, There is no problem with the conversion of a parent object to a subclass object or a subclass object into a parent object, so polymorphic applications are not so obvious; so-called polymorphism refers to the ability of a program to handle multiple types of objects, such as working in a company, monthly payroll payments, and a payroll method, In the company of different employees or different positions of employees are issued through this method, but the wages are not the same. So there are many forms of the same payroll method. For an object-oriented program, polymorphism is to assign a class object to the parent class reference, and then call the parent class method, to execute the child class overrides the parent class of the method, but in PHP is a weak type, the object reference is the same without the parent class reference, or subclass reference.

Let's take a look at an example where the relationship between the parent and subclass objects is the first thing to use polymorphism. To make a shape interface or abstract class as a parent, there are two abstract methods, one method for calculating the circumference, the other is the method of the area, the subclass of this interface is a variety of different shapes, each shape has a perimeter and area, and because the parent class is an interface, Therefore, the subclass must implement the parent class of the two perimeter and area of the abstract method, the purpose is that each of the different shapes of subclasses obey the specification of the parent interface, all have to seek circumference and to find the area of the method.

The code is as follows Copy Code

Defines a shape interface with two abstract methods for subclasses to implement
Interface Shape
{
function area ();
function perimeter ();
}

Defines a rectangular subclass that implements the perimeter 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:". ($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 "Circumference of the circle is:". (2*3.14* $this->radius);
}
}

A reference to a shape assigned to a class rectangle object
$shape =new Rect (5, 10);

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

A reference to a shape assigned to a circle-like object
$shape =new Circular (10);

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

The above example executes the result:
The area of the rectangle is: 50
The perimeter of the rectangle is: 30
The area of the circle is: 314
The circumference of the circle is: 62.8

All right, about the three of them. We have a detailed introduction, we do not understand the Baidu can also be a bit oh.


http://www.bkjia.com/PHPjc/444696.html www.bkjia.com true http://www.bkjia.com/PHPjc/444696.html techarticle This article introduces the application examples of the application of the automatic loading class object serialization polymorphism in the object oriented in PHP, and the students who need to know can look at it. Auto-load class many developers ...

  • Related Article

    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.