对象方法调用VS类方法调用.最佳实践? [英] Object method call VS class method call. Best practice?

查看:83
本文介绍了对象方法调用VS类方法调用.最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 PHP 编写一个应用程序,并且在我调用工具箱的类中收集了一系列杂项函数

I am writing an app in PHP and I have gathered a collection of miscellaneous functions inside a class that I am calling toolbox

Class toolbox {
  public function misc($var) {
    return do_something($var);
  } 
}

我正在我的各种脚本中加载工具箱然后使用它,但我不确定最佳实践应该是什么...

I am loading the toolbox inside my various scripts and then using it, but I am not sure what the best practice should be...

选项 1:对象方法调用

Option 1: object method call

__construct() {
  require_once('toolbox.php');
  $this->ToolBox = new toolbox;
}

some_function($input){
  return $this->ToolBox->misc($input);
}

选项 2:类方法调用

__construct() {
  require_once('toolbox.php');
}

some_function($input){
  return ToolBox::misc($input);
}

是否有某种最佳实践?我认为我不需要工具箱的各种实例.

Is there some kind of best practice about that? I don't think that I need various instances of the toolbox.

推荐答案

使它们成为静态函数.

class Toolbox {
    static public function Tool1(){
    }
}
Toolbox::Tool1(); // Call the static method without instantiating the object

为什么是静态类而不是命名空间?

因为静态实用程序类趋于增长并且趋于许多.您可能有一个用于文件字符串数组, ... 等等.他们每个人都可以轻松地跳 5K 行甚至更多.但您总是使用它们吗?不!它们可能用于您网站上 5% 的页面.那么为什么首先要包含它们?

Because static utility classes tend to grow and tend to be many. You might have one for files, strings, arrays, ... etc. Each of them can easily jump 5K lines or even more. But do you always use them? NO! They might be used in 5% of the pages on your site. So why include them in the first place?

PHP 的自动加载^ 在这里为我们提供帮助.但是自动加载仅适用于类,而必须包含命名空间.您现在有三个选择:

PHP's Autoload^ comes to our aid here. But autoload only works with classes while namespaces have to be included. You have three choices now:

  • include/require 一切,所以你确保你的命名空间总是可访问的,但你强制PHP在每次运行时解析大量文件,并且你获得了固有的性能实际上包括/需要一个文件的惩罚
  • 您将 include_once/require_once 保存在您使用实用程序类的每个 .php 文件中
  • 您使用 spl_autoload_register 而忘记了依赖关系,而自动加载器会在后台为您完成繁重的工作
  • you include / require EVERYTHING so you make sure your namespaces are always accessible but you force PHP to parse a lot of files on each run plus you get the inherent performance penalty of actually including / requiring a file
  • you keep include_once / require_once in each .php file where you use your utility classes
  • you use the spl_autoload_register and forget about dependencies while the autoloader does the heavy lifting in the background for you

前两个可以使用命名空间,而最后一个需要静态类.当你的代码库增长很多时,你开始欣赏 autoload 并且你开始欣赏静态类,不管其他人告诉你什么.所以:

First two can work with namespaces while the last needs a static class. When your code base grows A LOT you start appreciating autoload and you start appreciating static classes no matter what OTHERS tell you. So:

best practices == guidelines && best practices != mandatory practices
best practices != what works best in your specific scenario

^ 现在准备反对票 :)

这篇关于对象方法调用VS类方法调用.最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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