PHP中小代码重用的最佳实践 [英] Best practice for tiny code reuse in PHP

查看:134
本文介绍了PHP中小代码重用的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间我有一个问题 - 我应该重用小部分的代码,如果是这样,我应该怎么做,这将是最好的做法。

For a long time I have a problem - should I reuse small parts of code and if so, how should I do it so it would be the best practice.

我的意思是小代码是例如:

What I mean about small code is for example:

if (!is_array($table)) {
    $table = array($table);  
}

$x = explode("\n", $file_content);

$lines = array();

for ($i=0, $c = count($x); $i<$c; ++$i) {
  $x[$i] = trim($x[$i]);
  if ($x[$i] == '') {
     continue;
  }          
  $lines[] = $x[$i];      
}

这些小部分代码可能在一个项目的许多类中使用,

Such tiny parts of code may be used in many classes in one project but some of them are used also in many projects.

有很多可能的解决方案:

There are many possible solutions I think:


  1. 创建简单的函数文件,并将所有可重用的代码块作为函数,在项目中包括它们,并在需要时使用它们

  2. 为这些代码创建特征

  3. 其他
  4. $ b $ b
  1. create simple function file and put them all reusable piece of codes as function, include them simple in project and use them whenever I want
  2. create traits for those piece of codes and use them in classes
  3. reuse code by simple copy paste or creating function in specific class (??)
  4. other ?

我认为所有这些解决方案都有自己的优点和缺点。

I think all of those solutions have their pros and cons.

问题:我应该使用什么方法(如果有的话)重复使用此类代码,为什么这种方法是您认为最好的? / p>

Question: What method should I use (if any) to reuse such code and why is this approach the best one in your opinion?

推荐答案

我认为最好的方法取决于很多因素,包括你的应用程序使用的技术它们运行的​​PHP等。例如,traits是有趣和有用的,但它们只有从php 5.4.0可用,所以使用这个工具来分组你的代码片段,你将无法在早期PHP版本的系统中重复使用它们。另一方面,如果您的应用程序使用OOP样式,并且您在功能中组织了可复用的小代码片段,那么它们的使用在OOP应用程序中看起来很不方便,并且与特定类中的函数名称冲突。在这种情况下,我认为在类中分组你的函数看起来更自然。

I think that "the best way" depends on many factors including the technology your applications use (procedural, OOP), versions of PHP they run on, etc. For example, traits are interesting and useful but they are available only since php 5.4.0 so using this tool to group your code snippets you will not be able to reuse them in systems running on earlier PHP versions. On the other hand if your app uses an OOP style and you organized your resuable small code snippets in functions, their usage may seem awkward in an OOP app and conflict with the function names in a particular class. In this case I think grouping your functions in classes would seem more natural.

把所有东西放在一起,似乎类提供了更好的工具用于分组resiable代码片段,即与早期的PHP版本的向后兼容性,避免功能名称冲突等)个人我主要在OOP中编码,所以我有一个Util类,其中我分组小功能代表可重用的代码片断,彼此不直接相关,因此无法在其他类中逻辑分组。

Putting everything together, it seems that classes provide better tool for grouping resuable code snippets in terms outline above, namely backward compatibility with earlier PHP versions, avoiding function names conflicts, etc.) Personally I code mostly in OOP, so i have a Util class where I group small functions representing resuable pieces of code snippets that do not directly relate to each other and thus could not be logically groupped in other classes.

这篇关于PHP中小代码重用的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆