Explanation of original sin based on PHP static classes

Source: Internet
Author: User

There is a famous saying in iegg: existence is reasonable. The use of static classes is justified based on this argument. However, it is absolutely necessary. Once the code is too dependent on the static class, its deterioration is inevitable. This is like poppy, as a herb, has its pharmacological value, but it becomes a drug if it is used in large quantities.

What is a static class?

Static classes refer to classes that are called directly in static mode without being converted into objects by instance.The Code is as follows:
Copy codeThe Code is as follows:
<? Php

Class Math
{
Public static function ceil ($ value)
{
Return ceil ($ value );
}

Public static function floor ($ value)
{
Return floor ($ value );
}
}

?>

At this time, the role played by the class is more like a namespace, which may be the most direct reason many people prefer static classes.

Static class problems

Essentially, static classes are process-oriented, because they usually combine the originally process-oriented code sets mechanically. Although the results exist in the form of classes, however, at this time, the class is more like a new emperor, so it can be said that the static class is actually dressed in an object-oriented shell, doing process-oriented things.

One of the object-oriented design principles: interface-oriented programming, rather than implementation programming. What is the difference? For example: aside from the price, do you like computers with independent graphics cards or computers with integrated graphics cards? I think most people choose independent graphics cards. Independent graphics cards can be viewed as interface-oriented programming, while integrated graphics cards can be viewed as implementation programming. In this case, the disadvantages of programming are suddenly put on the paper: it loses the possibility of change.

The following is an example of a management system:
Copy codeThe Code is as follows:
<? Php

Class Article
{
Public function save ()
{
ArticleDAO: save ();
}
}

?>

Article implements the necessary domain logic and then delivers data persistence to ArticleDAO. ArticleDAO is a static class, which is as hard to change as the integrated video card that is soldered on the motherboard, suppose we may need to implement Mock ArticleDAO to test the code, but because the static class name is used for calling, it is equivalent to the specific implementation method already bound, Mock is almost impossible, of course, there are actually some ways to achieve this:
Copy codeThe Code is as follows:
<? Php

Class Article
{
Private static $ dao = 'articledao ';

Public static funciton setDao ($ dao)
{
Self: $ dao = $ dao;
}

Public static function save ()
{
$ Dao = self: $ dao;

$ Dao: save ();
}
}

?>

With the intervention of variables, you can set which static class to use at runtime:
Copy codeThe Code is as follows:
<? Php

Article: setDao ('mockarticledao ');

Article: save ();

?>

Although this implementation method seems to solve the Mock problem, the original code it modified first violates the open and closed principles, and secondly introduces static variables, static variables are shared and may interfere with the execution of other code, so they are not a perfect solution.

In addition, the dynamic language features can be used to implement Mock simply by using a different class definition file of require. However, this method also has drawbacks, suppose we need to change the implementation method multiple times in the script, but in fact we only have one chance of require. Otherwise, we will see repeated definition errors.


Object Value

How can I implement the document management system example if I abandon static classes and use objects instead?The Code is as follows:
Copy codeThe Code is as follows:
<? Php

Class Article
{
Private $ dao;

Public function _ construct ($ dao = null)
{
If ($ dao = null ){
$ Dao = new ArticleDAO ();
}

$ This-> setDao ($ dao );
}

Public function setDao ($ dao)
{
$ This-> dao = $ dao;
}

Public function save ()
{
$ This-> dao-> save ();
}
}

?>

In fact, here we use the dependency injection technology that people often call to inject dependent objects through the constructor or Setter:
Copy codeThe Code is as follows:
<? Php

$ Article = new Article (new MockArticleDAO ());

$ Article-> save ();

?>

The object has its own status, so the sharing status does not interfere with the execution of other code.

...

Of course, static classes have a good side. For example, they are suitable for implementing some stateless tool classes. However, most of the time, my subjective tendency is very clear. I use more objects and use less static classes, avoid Premature system solidification. By the way, I hope no one will tell me about the preaching of static analogy objects such as quick object. Thank you.

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.