Original sin of static classes

Source: Internet
Author: User
There is a famous saying in iegg: existence is reasonable. The existence of static classes is justified. 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 static class> <LINKhref = "http://www.php100.com//st

There is a famous saying in iegg: existence is reasonable. The existence of static classes is justified. 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:

 
 
  1.  
  2. class Math  
  3. {  
  4.     public static function ceil($value)  
  5.     {  
  6.         return ceil($value);  
  7.     }  
  8.  
  9.     public static function floor($value)  
  10.     {  
  11.         return floor($value);  
  12.     }  
  13. }  
  14.  
  15. ?> 

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 object-oriented Pier, 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:

 
 
  1.  
  2. class Article  
  3. {  
  4.     public function save()  
  5.     {  
  6.         ArticleDAO::save();  
  7.     }  
  8. }  
  9.  
  10. ?> 

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:

 
 
  1.  
  2. class Article  
  3. {  
  4.     private static $dao = 'ArticleDAO';  
  5.  
  6.     public static funciton setDao($dao)  
  7.     {  
  8.         self::$dao = $dao;  
  9.     }  
  10.  
  11.     public static function save()  
  12.     {  
  13.         $dao = self::$dao;  
  14.  
  15.         $dao::save();  
  16.     }  
  17. }  
  18.  
  19. ?> 

With the intervention of variables, you can set which static class to use at runtime:

 
 
  1.  
  2. Article::setDao('MockArticleDAO');  
  3.  
  4. Article::save();  
  5.  
  6. ?> 

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.

Note: In some cases, static latency binding can also improve the testability of static classes. For more information, see PHPUnit.

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:

 
 
  1.  
  2. class Article  
  3. {  
  4.     private $dao;  
  5.  
  6.     public function __construct($dao = null)  
  7.     {  
  8.         if ($dao === null) {  
  9.             $dao = new ArticleDAO();  
  10.         }  
  11.  
  12.         $this->setDao($dao);  
  13.     }  
  14.  
  15.     public function setDao($dao)  
  16.     {  
  17.         $this->dao = $dao;  
  18.     }  
  19.  
  20.     public function save()  
  21.     {  
  22.         $this->dao->save();  
  23.     }  
  24. }  
  25.  
  26. ?> 

In fact, here we use the dependency injection technology that people often call to inject dependent objects through the constructor or Setter:

 
 
  1.  
  2. $article = new Article(new MockArticleDAO());  
  3.  
  4. $article->save();  
  5.  
  6. ?> 

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.