将模块的 PHP 代码拆分为单独的包含文件
Split PHP code of a module into separated include files
我有一段代码需要在我的应用程序的很多地方使用。
示例:
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
现在,它们是我的 10
个控制器。
如果我需要修复任何东西,我需要修复 10
个地方。
我正在寻找更好的方法来控制它。
我想到写一个函数
public static function get_device_info($cpe_mac){
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
}
当我调用该函数时:$devices = get_device_info($cpe_mac);
我只能访问 1 个变量,即 $devices
。
我无法访问该函数中的所有 5
变量。
- $count_device
- $c_devices
- $c_active
- $c_inactive
- $c_offline
我找到了 get_defined_vars,但这并不是我真正要找的。
问题
如何着手实施?
如何移动代码块并将其包含回去?
我应该开始研究 PHP 的 require/include 吗?
我一直这样做,尤其是在整个网站上使用相同的 header/footer。只需将此行放在您要在其中要求代码的文档中。
<?php require_once('php/code_block_one.php'); ?>
如果要在单个页面中多次调用同一代码块,请将require_once
更改为require
。
使用传递引用。
public static function
get_device_info($cpe_mac, &$count_device, &$c_devices, &$c_active, &$c_inactive, &$c_offline){
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
}
// now to call this function...
Clazz::get_device_info("cpe_mac", $count_device, $c_devices, $c_active, $c_inactive, $c_offline);
var_dump($count_device, $c_devices, $c_active, $c_inactive, $c_offline);
// they output useful data!
此外,根据您的用例,如果您的 PHP 代码没有直接从源代码部署到服务器(例如,如果您发布了打包的 phars 等),您可能需要使用 cpp
(C 预处理器)来预处理您的文件。
//METHOD 1
public static $c_devices = null;
public static $c_active = null;
public static $c_inactive = null;
public static $c_offline = null;
public static function get_device_info($cpe_mac){
$count_device = VSE::count_device($cpe_mac);
self::$c_devices = $count_device['Count_of_devices'];
self::$c_active = $count_device['Count_of_active'];
self::$c_inactive = $count_device['Count_of_inactive'];
self::$c_offline = $count_device['Count_of_offline'];
}
ClassName::$c_devices;
ClassName::$c_active;
ClassName::$c_inactive;
ClassName::$c_offline;
//METHOD 2
public static $cpe_mac = null;
public static function get_device_info($key){
$count_device = VSE::count_device(self::$cpe_mac);
return $count_device[$key];
}
ClassName::$cpe_mac = $cpe_mac;
ClassName::get_device_info('Count_of_devices');
ClassName::get_device_info('Count_of_active');
ClassName::get_device_info('Count_of_inactive');
ClassName::get_device_info('Count_of_offline');
您可以将所有内容包装在 DeviceInfo class 中,然后只需使用 class 上的属性。
class DeviceInfo
{
public $c_devices;
public $c_active;
public $c_inactive;
public $c_offline;
public function __construct($cpe_mac) {
$count_device = VSE::count_device($cpe_mac);
$this->c_devices = $count_device['Count_of_devices'];
$this->c_active = $count_device['Count_of_active'];
$this->c_inactive = $count_device['Count_of_inactive'];
$this->c_offline = $count_device['Count_of_offline'];
}
}
将 Class 放在自己的名为 DeviceInfo.php 的文件中,然后在您需要的地方
include_once("DeviceInfo.php");
在文件的顶部并创建该 class 的新实例。 (我使用 include_once 来确保 DeviceInfo class 在已经定义的情况下不会被重新定义)
$deviceInfo = new DeviceInfo($cpe_mac);
您可以像这样访问属性来访问这些值。
$deviceInfo->c_devices;
通过这种方式,您可以获得值的代码完成(取决于您的 IDE),并且当您真正想在代码中使用该信息时,不必依赖于记住数组键名。
如果您想更进一步,您甚至可以向此 class 添加 getter 函数,因此如果将来您需要更改这些值的计算或获取方式,而无需改变 API 就简单多了。那看起来像这样:
class DeviceInfo
{
protected $c_devices;
protected $c_active;
protected $c_inactive;
protected $c_offline;
public function get_c_devices() {
return $this->c_devices;
}
public function get_c_active() {
return $this->c_active;
}
public function get_c_inactive() {
return $this->c_inactive;
}
public function get_c_offline() {
return $this->c_offline;
}
public function __construct($cpe_mac) {
$count_device = VSE::count_device($cpe_mac);
$this->c_devices = $count_device['Count_of_devices'];
$this->c_active = $count_device['Count_of_active'];
$this->c_inactive = $count_device['Count_of_inactive'];
$this->c_offline = $count_device['Count_of_offline'];
}
}
这里唯一的区别是,现在要获取值,您需要调用函数,而不是像这样直接访问属性:
$deviceInfo = new DeviceInfo($cpe_mac);
$deviceInfo->get_c_devices(); // returns devices
举个这么简单的例子,额外的代码可能不值得,但这确实使将来更新此代码更容易,而不会破坏在应用程序的其余部分调用这些函数的所有要点。
如果您不想更改页面上的任何变量并且您可能正在考虑使用全局函数,您可以:
function get_device_info($cpe_mac)
{
$count_device = VSE::count_device($cpe_mac);
return [
'c_devices' => $count_device['Count_of_devices'],
'c_active' => $count_device['Count_of_active'],
'c_inactive' => $count_device['Count_of_inactive'],
'c_offline' => $count_device['Count_of_offline'],
];
}
然后你会打电话给:
extract(get_device_info($someVar));
您将有权访问:
$c_devices;
$c_active;
$c_inactive;
$c_offline;
就像你一直做的那样
请注意,我并不是说这个答案比其他答案更好,我只是说这是一个替代方案。
希望对您有所帮助!
我有一段代码需要在我的应用程序的很多地方使用。
示例:
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
现在,它们是我的 10
个控制器。
如果我需要修复任何东西,我需要修复 10
个地方。
我正在寻找更好的方法来控制它。
我想到写一个函数
public static function get_device_info($cpe_mac){
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
}
当我调用该函数时:$devices = get_device_info($cpe_mac);
我只能访问 1 个变量,即 $devices
。
我无法访问该函数中的所有 5
变量。
- $count_device
- $c_devices
- $c_active
- $c_inactive
- $c_offline
我找到了 get_defined_vars,但这并不是我真正要找的。
问题
如何着手实施?
如何移动代码块并将其包含回去?
我应该开始研究 PHP 的 require/include 吗?
我一直这样做,尤其是在整个网站上使用相同的 header/footer。只需将此行放在您要在其中要求代码的文档中。
<?php require_once('php/code_block_one.php'); ?>
如果要在单个页面中多次调用同一代码块,请将require_once
更改为require
。
使用传递引用。
public static function
get_device_info($cpe_mac, &$count_device, &$c_devices, &$c_active, &$c_inactive, &$c_offline){
$count_device = VSE::count_device($cpe_mac);
$c_devices = $count_device['Count_of_devices'];
$c_active = $count_device['Count_of_active'];
$c_inactive = $count_device['Count_of_inactive'];
$c_offline = $count_device['Count_of_offline'];
}
// now to call this function...
Clazz::get_device_info("cpe_mac", $count_device, $c_devices, $c_active, $c_inactive, $c_offline);
var_dump($count_device, $c_devices, $c_active, $c_inactive, $c_offline);
// they output useful data!
此外,根据您的用例,如果您的 PHP 代码没有直接从源代码部署到服务器(例如,如果您发布了打包的 phars 等),您可能需要使用 cpp
(C 预处理器)来预处理您的文件。
//METHOD 1
public static $c_devices = null;
public static $c_active = null;
public static $c_inactive = null;
public static $c_offline = null;
public static function get_device_info($cpe_mac){
$count_device = VSE::count_device($cpe_mac);
self::$c_devices = $count_device['Count_of_devices'];
self::$c_active = $count_device['Count_of_active'];
self::$c_inactive = $count_device['Count_of_inactive'];
self::$c_offline = $count_device['Count_of_offline'];
}
ClassName::$c_devices;
ClassName::$c_active;
ClassName::$c_inactive;
ClassName::$c_offline;
//METHOD 2
public static $cpe_mac = null;
public static function get_device_info($key){
$count_device = VSE::count_device(self::$cpe_mac);
return $count_device[$key];
}
ClassName::$cpe_mac = $cpe_mac;
ClassName::get_device_info('Count_of_devices');
ClassName::get_device_info('Count_of_active');
ClassName::get_device_info('Count_of_inactive');
ClassName::get_device_info('Count_of_offline');
您可以将所有内容包装在 DeviceInfo class 中,然后只需使用 class 上的属性。
class DeviceInfo
{
public $c_devices;
public $c_active;
public $c_inactive;
public $c_offline;
public function __construct($cpe_mac) {
$count_device = VSE::count_device($cpe_mac);
$this->c_devices = $count_device['Count_of_devices'];
$this->c_active = $count_device['Count_of_active'];
$this->c_inactive = $count_device['Count_of_inactive'];
$this->c_offline = $count_device['Count_of_offline'];
}
}
将 Class 放在自己的名为 DeviceInfo.php 的文件中,然后在您需要的地方
include_once("DeviceInfo.php");
在文件的顶部并创建该 class 的新实例。 (我使用 include_once 来确保 DeviceInfo class 在已经定义的情况下不会被重新定义)
$deviceInfo = new DeviceInfo($cpe_mac);
您可以像这样访问属性来访问这些值。
$deviceInfo->c_devices;
通过这种方式,您可以获得值的代码完成(取决于您的 IDE),并且当您真正想在代码中使用该信息时,不必依赖于记住数组键名。
如果您想更进一步,您甚至可以向此 class 添加 getter 函数,因此如果将来您需要更改这些值的计算或获取方式,而无需改变 API 就简单多了。那看起来像这样:
class DeviceInfo
{
protected $c_devices;
protected $c_active;
protected $c_inactive;
protected $c_offline;
public function get_c_devices() {
return $this->c_devices;
}
public function get_c_active() {
return $this->c_active;
}
public function get_c_inactive() {
return $this->c_inactive;
}
public function get_c_offline() {
return $this->c_offline;
}
public function __construct($cpe_mac) {
$count_device = VSE::count_device($cpe_mac);
$this->c_devices = $count_device['Count_of_devices'];
$this->c_active = $count_device['Count_of_active'];
$this->c_inactive = $count_device['Count_of_inactive'];
$this->c_offline = $count_device['Count_of_offline'];
}
}
这里唯一的区别是,现在要获取值,您需要调用函数,而不是像这样直接访问属性:
$deviceInfo = new DeviceInfo($cpe_mac);
$deviceInfo->get_c_devices(); // returns devices
举个这么简单的例子,额外的代码可能不值得,但这确实使将来更新此代码更容易,而不会破坏在应用程序的其余部分调用这些函数的所有要点。
如果您不想更改页面上的任何变量并且您可能正在考虑使用全局函数,您可以:
function get_device_info($cpe_mac)
{
$count_device = VSE::count_device($cpe_mac);
return [
'c_devices' => $count_device['Count_of_devices'],
'c_active' => $count_device['Count_of_active'],
'c_inactive' => $count_device['Count_of_inactive'],
'c_offline' => $count_device['Count_of_offline'],
];
}
然后你会打电话给:
extract(get_device_info($someVar));
您将有权访问:
$c_devices;
$c_active;
$c_inactive;
$c_offline;
就像你一直做的那样
请注意,我并不是说这个答案比其他答案更好,我只是说这是一个替代方案。
希望对您有所帮助!