_php example of original crime based on PHP static class

Source: Internet
Author: User
Hegel has a famous saying: existence is reasonable. With this as an argument, the use of static classes must have its rationality. But extremes meet, once the code is too dependent on static classes, its deterioration of the outcome is inevitable. This is like the poppy as a herb, has its pharmacological value, but if unscrupulous use of large, it becomes a drug.

What is a static class

The so-called static class refers to a class that is called directly by a static method without instantiating it into an object. The code is as follows:
Copy the Code code as follows:

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

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

?>

The role of the class at this time is more like a namespace, which is perhaps the most straightforward reason many people prefer to use static classes.

Problems with static classes

In essence, static classes are process-oriented, because usually it is simply a mechanical process-oriented code together, although the result is in the form of a 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 in the object-oriented shell, doing the process-oriented thing.

One of the object-oriented design principles: programming for interfaces, not for implementation. What's the difference? For example: Aside from the price factor, do you prefer a computer with a discrete graphics card or a computer with integrated graphics? I think most people will choose a discrete graphics card. Discrete graphics can be seen as programming for interfaces, and integrated graphics can be seen as programming for implementation. So the disadvantage of implementing programming is leap off: it loses the possibility of change.

Here is an example of an article management system to specify:
Copy the Code code as follows:

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

?>

Article implements the necessary domain logic and then gives the data persistence to Articledao, and Articledao is a static class that is as difficult to change as an integrated video card welded to the motherboard, assuming that we may need to mock out the implementation of Articledao in order to test the code , but since the invocation uses the name of a static class, which is equivalent to a specific implementation that has already been bound, the mock is almost impossible, and there are, of course, some methods that can be implemented:
Copy the Code code as follows:

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 involvement of variables, you can set which static class to use at run time:
Copy the Code code as follows:

Article::setdao (' Mockarticledao ');

Article::save ();

?>

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

In addition, the use of dynamic language features, in fact, can be simple by require a different class definition file to achieve the mock, but this is also a disadvantage, imagine that we need to change the implementation of the script many times, but in fact we have only one require chance, Otherwise, a duplicate-defined error will occur.


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:
Copy the Code code 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, the use of dependency injection technology, which is often said, is used to inject dependent objects through a constructor or setter:
Copy the Code code as follows:

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

$article->save ();

?>

The object has its own state and does not occur when shared state interferes with execution of other code.

...

Of course, the static class has a good side, for example, is very suitable for some stateless tool classes, but most of the time, my subjective inclination is very clear, multi-use objects, less static classes, to avoid premature curing system. By the way, I hope I don't have someone to tell me about the static analogy object fast or something, 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.