将模块的PHP代码拆分为单独的包含文件

时间:2016-10-31 16:04:39

标签: php dry require-once

我有一段代码需要在我的应用程序的很多地方使用。

示例:

$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吗?

我现在正在接受任何建议。

任何提示/建议/帮助都将非常感谢!

5 个答案:

答案 0 :(得分:4)

我一直这样做,特别是在网站上使用相同的页眉/页脚。只需将此行放在要求代码的文档中即可。

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

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

答案 1 :(得分: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'];
    }
}

将Class放在自己的文件中,名为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

举个简单的示例,额外的代码可能不值得,但这确实可以让以后更容易更新这些代码而不会破坏在应用程序其余部分调用这些函数的所有要点。

答案 2 :(得分: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;

就像你一直做的那样

请注意,我并不是说这是一个比其他人提供的更好的答案,我只是说它是另一种选择。

希望这有帮助!

答案 3 :(得分: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预处理器)来预处理你的文件。

答案 4 :(得分: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');