duanlaican1849 2017-01-24 14:15
浏览 60
已采纳

如果我们只调用一次函数,我们应该使用'use'吗?

Is it bad to use the keyword use even though we only call a function once? E.g. I have my own typo3 extension, and I am accessing a typo3 core function in my controller, but only once.

$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');

I could also make use of use:

use \TYPO3\CMS\Core\Utility\GeneralUtility;
...
$message = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');

In my opinion the second variant is much cleaner, but are there any performance issues if I make use of use only for one call?

  • 写回答

4条回答 默认 最新

  • dparivln22034 2017-01-24 14:17
    关注

    In theory, there should be no performance hit. You're calling a function (and thus an autoloader) either way (which is where any performance hit will be).

    To explain why, your use statement is simply an alias. Your autoloader will do the same work either way. And it will likely be opcode cached, so any performance hit (we're talking milliseconds here, if that) will only be on the first run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题