将一个模块的 PHP 代码拆分成单独的 include 文件。

5
我有一段代码需要在我的应用程序的很多地方使用。
例如:
$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);,我只能访问一个变量$devices,而无法在该函数中访问所有的5个变量:
- $count_device - $c_devices - $c_active - $c_inactive - $c_offline 我找到了get_defined_vars,但那并不是我要找的。
问题:
- 有什么方法可以实现这一点? - 如何移动一块代码并将其包含回来? - 我应该开始研究PHP的require/include吗?

6
为什么你需要设置所有这些变量,不能只使用数组吗?这样似乎更容易。 - jeroen
5个回答

4

我经常这样做,特别是想在整个网站上使用相同的页眉/页脚。只需在您想要引用该代码的文档中放置此行即可。

<?php require_once('php/code_block_one.php'); ?>

如果您想在同一页面中多次调用相同的代码块,请将require_once更改为require

这将只运行一次代码。如果您调用两次,它将不再运行。不要使用 require_once。请改用 require - SOFe
如果您将每个代码片段放在单独的文件中,那么这也是一种糟糕的代码结构。文件很容易混乱。 - SOFe
在我发布编辑之前,您已经发表了评论。虽然我想不出在文档中需要两次引用函数或重复变量声明的情况,但我已更新答案以解决这个问题。 - Timothy R
1
这是非常有可能的。例如,您可以在循环中运行此代码。 - SOFe

4

您可以将所有内容封装在DeviceInfo类中,然后只需使用该类上的属性即可。

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'];
    }
}

在自己的文件DeviceInfo.php中编写这个类,然后在需要使用时调用即可。
include_once("DeviceInfo.php");

在文件顶部引入这个类,并创建该类的一个新实例。(我使用include_once来确保如果它已经被定义,DeviceInfo类不会被重新定义)

$deviceInfo = new DeviceInfo($cpe_mac);

您可以通过访问属性来访问这些值,就像这样。
$deviceInfo->c_devices;

这样一来,您就可以得到值的代码补全(取决于您使用的IDE),在实际使用该信息时不必依赖于记忆数组键名。
如果您想更进一步,还可以为此类添加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

对于这个简单的示例,额外的代码可能不值得,但是这样做可以更容易地更新未来的代码,而不会破坏应用程序中调用这些函数的所有点。


4

如果您不想更改页面上的任何变量,并且正在考虑使用全局函数,可以执行以下操作:

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;

像你一直做的那样

请注意,我并不是说这个答案比其他提供的更好,我只是在说这是一个替代方案。

希望这能有所帮助!


我想补充一点,如果他想要更改变量,他只需要将 extract(get_device_info($someVar)); 替换为 $info = get_device_info($someVar); 并使用 $info['c_devices']$info['c_active'] (...) 代替 $c_devices$c_active (...)。 - Manuel Mannhardt
1
如果您不想改变现有的大量代码,这是一个很好的解决方案。 - Joel Bell

2

使用按引用传递。

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预处理器)来预处理您的文件。


1
//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');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接