In php, a function only uses two or three times in multiple classes. Where should I put this function?

Source: Internet
Author: User
There is a function in the project that is used only three times in several classes. Do I put this function in a common function library or in each class? Isn't it common enough to put the public function library into use, and it seems cumbersome to put it in the class? How do we deal with less-called functions? There is a function in the project that is used only three times in several classes. Do I put this function in a common function library or in each class?

Isn't it common enough to put the public function library into use, and it seems cumbersome to put it in the class?

How do we deal with less-called functions?

Reply content:

There is a function in the project that is used only three times in several classes. Do I put this function in a common function library or in each class?

Isn't it common enough to put the public function library into use, and it seems cumbersome to put it in the class?

How do we deal with less-called functions?

I think all repeated code should be put in the public place. You have already used it three times, and you will need to use it later. In addition, you can endure putting a public east and west into three classes. If you can, you can write a parent class, put this function in it, and inherit the necessary class to use it.

Just a few functions can be written as a trait.

Every code that has been repeated is extracted to form a static class method or function, because you have already repeated the code, and you have to use it again after you can't help it.

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.