Ways to develop large PHP projects (II.)

Source: Internet
Author: User
Tags define arrays constructor copy explode html form variables php and
Project polymorphism
Polymorphism is the ability of an object to decide which object to invoke, based on the parameters of the object passed at run time. For example
If you have a figure class, it defines a draw method. and derived the circle and rectangle classes, in the derived class you override
Covered the draw method, you might also have a function that expects to use a parameter X and can call $x->draw (). If you have polymorphism,
Which draw method is invoked depends on the type of object you pass to the function.

Polymorphism in an interpreted language like PHP (imagine a C + + compiler generating such code, which method should you call?) You
Also do not know what type of object you have, good, this is not the point is very easy and natural. So PHP certainly supports polymorphism.



<?php

function nicedrawing ($x) {

Suppose this is a method of the Board class
$x->draw ();
}

$obj =new Circle (3,187);
$obj 2=new Rectangle (4,5);

$board->nicedrawing ($obj);
The draw method that will invoke circle

$board->nicedrawing ($obj 2);
The draw method that will invoke rectangle

?>


Object-oriented Programming with PHP
Some "purists" (purists) may say that PHP is not a true object-oriented language, which is true. PHP is a mixed type
Language, you can use OOP, or you can use traditional procedural programming. However, for large projects, you may want/need to use in PHP
Pure OOP declares classes, and only objects and classes are used in your project.

As the project grows, using OOP can be helpful, and OOP code is easy to maintain, easy to understand and reuse. These are software engineering.
of the foundation. Applying these concepts to web-based projects is the key to success in future Web sites.
Advanced OOP techniques in PHP
After looking at basic OOP concepts, I can show you more advanced technologies:

Serialization (serializing)
PHP does not support persistent objects, and in OOP, permanent objects are objects that can maintain state and functionality in multiple application references, which means holding
There is the ability to save objects to a file or database, and you can load objects at a later time. This is called the serialization mechanism. PHP hold
There is a serialization method that can be invoked through an object, and the serialization method can return the string representation of an object. However, serialization only preserves the
The member data of the object without wrapping the method.

In PHP4, if you serialize an object into a string $s, then release the object and then deserialize the object to $obj, you can continue
How to use Objects! I do not recommend doing this because (a) there is no guarantee in the document that this behavior can still be used in future releases. (b)
This can lead to a misunderstanding when you save a serialized version to disk and exit the script. When you run this script later, you
You can't expect the object's method to be there when you deserialize an object, because the string representation does not include the method at all.

In summary, the serialization of PHP is useful for saving the member variables of an object. (You can also serialize related arrays and arrays to
A file).

Example:

-----------------------------------------------------
<?php

$obj =new Classfoo ();
$str =serialize ($obj);

Save $str to disk


A few months later


Mount Str from disk

$obj 2=unserialize ($STR)

?>---------------------------------------------------
  
You have restored the member data, but not the method (according to the document). This leads to access only through a similar use of $obj2->x
Member variables (you have no other way!) The only way, so don't try it at home.

There are some ways to solve this problem, I keep it, because they are too bad for this concise article.

I would be happy to welcome the full serialization feature in subsequent versions of PHP.

Using classes for data storage
One of the great things about PHP and OOP is that you can easily define a class to manipulate something, and whenever you want to
You can call the corresponding class when you use it. If you have an HTML form, the user can select a product by selecting the Product ID number. In several
According to the library has the product information, you want to display the product, showing its price and so on. You have different types of products, and the same action
may have different meanings for different products. For example, displaying a sound might mean playing it, but for other kinds of products it might
means to display a picture that exists in the database. You can use OOP or PHP to reduce coding and improve quality:

Define a product's class, define the methods it should have (for example: Display), and then define the class for each type of product, from the product
Class pies come out (Sounditem class, Viewableitem class, and so on), and cover the methods in the product class to make them act as you think.

Name a class based on the Type field of each product in the database, and a typical product table might have (ID, type, price,
Description, etc. fields) ... Then in the processing script, you can take the type value out of the database and instantiate a name of type
The objects:



-----------------------------------------------------
<?php

$obj =new $type ();
$obj->action ();

?>---------------------------------------------------
 
This is a very good feature of PHP, you can call the $obj display method or other method without considering the type of object. Use
This technique, you don't need to modify the script to add a new type of object, just add a class that handles it.

This is a powerful feature, as long as you define the method without considering the types of all the objects, implement them in different ways in different classes, and then use them in the main script for any object, no if...else, no two programmers, just be happy.

Now you agree that programming is easy, maintenance is cheap, reusable is true?

If you manage a group of programmers, assigning work is simple, and each person may be responsible for one type of object and the class that handles it.

You can internationalize through this technology, apply the appropriate classes to the language fields that the user chooses, and so on.


Copy and Clone
When you create a $obj object, you can copy the object by $obj2= $obj, and the new object is a copy of $obj (not a
Reference), so it has a state of $obj at that time. Sometimes you don't want to, you just want to generate a new one like the Obj class
object, you can invoke the class's constructor by using the new statement. It can also be done in PHP by serialization, and a base class, but the
Some other classes are derived from the base class.



Enter the danger zone.
When you serialize an object, you get a string of some form, and if you're interested, you can tune it, where the string has
The name of the class (great!) ), you can take it out like this:

-----------------------------------------------------
<?php

$herring =serialize ($obj);
$vec =explode (': ', $herring);
$nam =str_replace ("\" ",", $vec [2]);

?>---------------------------------------------------
 
So suppose you create a "universe" class and force all classes to be extended from universe, you can universe
To define a clone in the following way:
-----------------------------------------------------
<?php

Class Universe {
function Clone () {
$herring =serialize ($this);
$vec =explode (': ', $herring);
$nam =str_replace ("\" ",", $vec [2]);
$ret =new $nam;
return $ret;
}
}

And then
$obj =new something ();

Extending from Universe
$other = $obj->clone ();

?>---------------------------------------------------
 
What you get is a new something class object that uses the same object that the constructor creates by using the new method. I don't
Knowing whether this is useful to you, but the Universe class can know the name of the derived class is a good experience. Imagination is the only limit.

Note: I use PHP4, I write some things under PHP3 may not work.

< finished full >


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.