我正在创建一个带有主题选项的简单框架。我将代码块划分为functions.php
并将其放置在特定的文件夹结构中。
现在在我的主要functions.php
文件,我只有require_once
调用这些文件。
但为了便于讨论,假设我最终将包含20个文件。
我正在创建一个带有主题选项的简单框架。我将代码块划分为functions.php
并将其放置在特定的文件夹结构中。
现在在我的主要functions.php
文件,我只有require_once
调用这些文件。
但为了便于讨论,假设我最终将包含20个文件。
1. 这对WP的性能有明显的影响吗?
如果它会对一些小文件产生真正的影响,那么它的影响将低于WP:PHP和服务器性能。这真的有影响吗?不是真的。但您仍然可以自己开始进行性能测试。
2. 是否最好将其全部保存在一个文件(functions.php)中
现在的问题是“什么更好”?从总文件加载时间开始?从文件组织的角度来看?不管怎样,这没什么区别。这样做的方式使你不会失去总体印象,并能以一种令你愉悦的方式保持结果。
3. 最好的方法是什么?
我通常做的只是在(plugins_loaded
, after_setup_theme
, 等等-取决于您需要什么),然后只需要全部:
foreach ( glob( plugin_dir_path( __FILE__ ) ) as $file )
require_once $file;
无论如何,你也可以让它变得更复杂、更灵活。看看这个例子:<?php
namespace WCM;
defined( \'ABSPATH\' ) OR exit;
class FilesLoader implements \\IteratorAggregate
{
private $path = \'\';
private $files = array();
public function __construct( $path )
{
$this->setPath( $path );
$this->setFiles();
}
public function setPath( $path )
{
if ( empty( $this->path ) )
$this->path = \\plugin_dir_path( __FILE__ ).$path;
}
public function setFiles()
{
return $this->files = glob( "{$this->getPath()}/*.php" );
}
public function getPath()
{
return $this->path;
}
public function getFiles()
{
return $this->files;
}
public function getIterator()
{
$iterator = new \\ArrayIterator( $this->getFiles() );
return $iterator;
}
public function loadFile( $file )
{
include_once $file;
}
}
这是一个基本相同的类(需要PHP 5.3+)。好处是它的粒度更细粒度,因此您可以轻松地从执行特定任务所需的文件夹中加载文件:$fileLoader = new WCM\\FilesLoader( \'assets/php\' );
foreach ( $fileLoader as $file )
$fileLoader->loadFile( $file );
更新,因为我们生活在一个新的后PHP v5中。2世界,我们可以利用\\FilterIterator
. 最短变体示例:$files = new \\FilesystemIterator( __DIR__.\'/src\', \\FilesystemIterator::SKIP_DOTS );
foreach ( $files as $file )
{
/** @noinspection PhpIncludeInspection */
! $files->isDir() and include $files->getRealPath();
}
如果您必须坚持使用PHP v5。2,那么你仍然可以\\DirectoryIterator
和几乎相同的代码。我根据我的需要重新设计了@kaiser answer,我想我可以分享它。我想要更多的选项,这些选项在代码和下面的使用示例中进行了解释。
Code:
<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Functions_File_Loader
*
* Makes it possible to clutter the functions.php into single files.
*
* @author kaiser
* @author ialocin
* @link http://wordpress.stackexchange.com/q/111970/22534
*
*/
class Functions_File_Loader implements IteratorAggregate {
/**
* @var array
*/
private $parameter = array();
/**
* @var string
*/
private $path;
/**
* @var string
*/
private $pattern;
/**
* @var integer
*/
private $flags;
/**
* @var array
*/
private $files = array();
/**
* __construct
*
* @access public
* @param array $parameter
*/
public function __construct( $parameter ) {
$this->set_parameter( $parameter );
$this->set_path( $this->parameter[ \'path\' ] );
$this->set_pattern( $this->parameter[ \'pattern\' ] );
$this->set_flags( $this->parameter[ \'flags\' ] );
$this->set_files();
}
/**
* set_parameter
*
* @access public
* @param array $parameter
*/
public function set_parameter( $parameter ) {
if ( empty( $parameter ) )
$this->parameter = array(\'\',\'\',\'\');
else
$this->parameter = $parameter;
}
/**
* get_parameter
*
* @access public
* @return array
*/
public function get_parameter() {
return $this->parameter;
}
/**
* set_path
*
* defaults to get_stylesheet_directory()
*
* @access public
* @param string $path
*/
public function set_path( $path ) {
if ( empty( $path ) )
$this->path = get_stylesheet_directory().\'/\';
else
$this->path = get_stylesheet_directory().\'/\'.$path.\'/\';
}
/**
* get_path
*
* @access public
* @return string
*/
public function get_path() {
return $this->path;
}
/**
* set_pattern
*
* defaults to path plus asterisk »*«
*
* @access public
* @param string $pattern
*/
public function set_pattern( $pattern ) {
if ( empty( $pattern ) )
$this->pattern = $this->get_path() . \'*\';
else
$this->pattern = $this->get_path() . $pattern;
}
/**
* get_pattern
*
* @access public
* @return string
*/
public function get_pattern() {
return $this->pattern;
}
/**
* set_flags
*
* @access public
* @param integer $flags
*/
public function set_flags( $flags ) {
if ( empty( $flags ) )
$this->flags = \'0\';
else
$this->flags = $flags;
}
/**
* get_flags
*
* @access public
* @return integer
*/
public function get_flags() {
return $this->flags;
}
/**
* set_files
*
* @access public
*/
public function set_files() {
$pattern = $this->get_pattern();
$flags = $this->get_flags();
$files = glob( $pattern, $flags );
$this->files = $files;
}
/**
* get_files
*
* @access public
* @return array
*/
public function get_files() {
return $this->files;
}
/**
* getIterator
*
* This function name has to be kept
*
* @access public
* @return void
*/
public function getIterator() {
$iterator = new ArrayIterator( $this->get_files() );
return $iterator;
}
/**
* load_file
*
* @access public
* @param string $file
*/
public function load_file( $file ) {
include_once $file;
}
}
$parameter = array(
// define path relative to get_stylesheet_directory()
// optional, defaults to get_stylesheet_directory()
\'path\' => \'includes/plugins\',
// optional, defaults to asterisk »*«
// matches all files ending with ».php«
// and not beginning with »_«, good for quickly deactivating
// directories searched are »path« and »subfolders«
// Additional examples:
// \'{*/,}{[!_],}func-*.php\' same as above but for files with a prefix
// \'[!_]*.php\' php files in defined »path«, not beginning with »_«
\'pattern\' => \'{*/,}[!_]*.php\',
// optional, defaults to 0
// needed if for example brackets are used
// more information: http://www.php.net/manual/en/function.glob.php
\'flags\' => GLOB_BRACE
);
// create object
$functionsfileloader = new Functions_File_Loader( $parameter );
// load the files
foreach ( $functionsfileloader as $file ) {
$functionsfileloader->load_file( $file );
}