The original sin of static class

Source: Internet
Author: User
Tags class definition variables require sin static class

Hegel has a famous saying: existence is reasonable. Using this as an argument, the use of static class must have its rationality. But extremes, once the code is too dependent on static classes, its degradation of the solution is inevitable. This is like the opium poppy as a herb, has its pharmacological value, but if unscrupulous use, it becomes a drug.

What is a static class

A static class refers to a class that is called directly by a static method without instantiating an object. The code is as follows:

Class Math
{
public static function Ceil ($value)
{
return Ceil ($value);
}

public static function Floor ($value)
{
Return floor ($value);
}
}

?>

The role that the class plays is more like a namespace, which is probably the most straightforward reason many people prefer to use static classes.

Problems with static classes

In essence, a static class is process-oriented, because it is usually simply a mechanical process-oriented code that brings together although the result is in the way of class, but at this time the class is more like an emperor's new clothes, so it can be said that the static class is actually draped in object-oriented shell, doing the process-oriented thing.

One of the object-oriented design principles: Programming for interfaces, not for implementation. What difference does it make? For example: Aside from the price factor, do you prefer a stand-alone graphics computer or a computer with integrated graphics? I think most people will choose a standalone video card. Discrete graphics can be seen as programming for interfaces, and integrated graphics can be seen as programming for implementation. So the drawbacks to programming are leaps: it loses the possibility of change.

The following is a fictional example of an article management system to specify:

Class Article
{
Public Function Save ()
{
Articledao::save ();
}
}

?>

Article implementation of the necessary domain logic, and then the data persisted to Articledao to do, and Articledao is a static class, as the integrated graphics on the motherboard as difficult to change, assuming that we have to test the code may need to mock out the implementation of Articledao , but because the call uses the name of the static class, which is equivalent to having already been bound to a specific implementation, the mock is almost impossible, and there are, of course, some methods that can be implemented:

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 a variable, you can set the specific static class to use at run time:

Article::setdao (' Mockarticledao ');

Article::save ();

?>

Although this implementation seems to solve the problem of mock, but first it modified the original code, violated the open and closed principle, followed by the introduction of static variables, and static variables are shared state, it may interfere with the execution of other code, so is not a perfect solution.

Add that, using the characteristics of dynamic language, can actually simply require a different class definition file to implement a mock, but this is also a disadvantage, imagine we need to change the implementation of the script many times, but in fact we have only one require opportunity, Otherwise, there will be a duplicate-defined error.

Note: In some cases, static delay binding can also be used to improve the testability of static classes, reference PHPUnit.

The value of the object

If you discard static classes and use objects instead, how do you implement an example of an article management system? The code is as follows:

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, there is a commonly used dependency injection technique that injects dependent objects through constructors or setters:

$article = new article (New Mockarticledao ());

$article->save ();

?>

The object has its own state and no shared state interferes with the execution of other code.

...

Of course, static classes have a good side, such as very suitable for the implementation of some stateless tool classes, but most of the time, my subjective tendency is very clear, multiple objects, less static class, to avoid premature curing system. By the way, I hope no one tells me about the static analogy object fast and so on the sermon, thank you. (Source: Fire Ding Notes)

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.