【问题标题】:Where to place require and use statements in a php class在 php 类中放置 require 和 use 语句的位置
【发布时间】:2014-01-26 11:56:08
【问题描述】:

我是 php 新手,我正在创建的程序没有按预期工作。我正在努力寻找错误,所以我想确保我将 require 和 use 语句放在正确的位置。在下面的代码示例中,require 和 use 语句的位置是否正确?还是应该在类定义中?

<?php

require 'abc.php'; 

use xyz;

class foo
{
  //or should I place both of them here?

  function bar ()

  {

  }
}
?>

【问题讨论】:

标签: php


【解决方案1】:

通常最好的做法是让他们在课堂之外Require 语句是自动加载的,namespacesuse-s 在外面。

例如查看这个现代框架:Nette: Compiler class。在 github 上浏览它,你会明白我在说什么。加载程序如下所示:Nette: Loader

【讨论】:

  • 你为什么这么认为? :)
  • 我刚刚打开 Nette: Compiler class. 并注意到 static 方法,ValidatorsConfig\HelpersStatement(和其他)类的紧密耦合。高圈复杂度和 npath 复杂度。这只是我查看的一个文件。
猜你喜欢
  • 2011-07-08
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
相关资源
最近更新 更多