Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PHP code of a module into separated include files

I have a block of code that I need to use in so many places of my app.

Example:

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

Right now, they in are 10 of my controllers. If I need to fix anything, I need to fix in 10 places.

I'm seeking a better way to control this.


I thought of writing a function

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

}

When I call that function: $devices = get_device_info($cpe_mac);

I only have access to 1 variable which is $devices.

I won't have access to all my 5 variables in that function.

  • $count_device
  • $c_devices
  • $c_active
  • $c_inactive
  • $c_offline

I've found get_defined_vars, but that is not really what I am looking for.


Questions

How would one go about and implement this?

How do I move a block of code and include it back?

Should I start look into PHP's require/include?

like image 654
code-8 Avatar asked Oct 31 '16 16:10

code-8


3 Answers

I do this all the time, particularly to use the same header/footer across the site. Simply place this line in the document in which you want to require the code.

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

If you want to call the same block of code multiple times in a single page, change require_once to require.

like image 69
Timothy R Avatar answered Nov 03 '22 05:11

Timothy R


If you don't want to change any of the variables on your page and you're potentially thinking about using a global function you could:

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

}

Then you would call:

extract(get_device_info($someVar));

and you would have access to:

$c_devices;
$c_active;
$c_inactive;
$c_offline;

Like you always have done

Please note I'm not saying this is a better answer than the others provided, I'm just saying it's an alternative.

Hope this helps!

like image 26
Rwd Avatar answered Nov 03 '22 05:11

Rwd


You could wrap everything in a DeviceInfo class then just use the properties on that 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'];
    }
}

Have the Class in its own file called DeviceInfo.php, then where you need it just

include_once("DeviceInfo.php");

at the top of the file and create a new instance of that class. (I use include_once to make sure the DeviceInfo class isn't redefined if its already been defined)

$deviceInfo = new DeviceInfo($cpe_mac);

You can access the values by accessing the properties like this.

$deviceInfo->c_devices;

This way you get code completion for the values (depending on your IDE) and don't have to rely on remembering the array key names when you actually want to use that info in your code.

if You want to take it a step further, you can even add getter functions to this class, so if in the future you need to change how these values are calculated or fetched without changing the API its a lot simpler. That would look something like this:

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

The only difference here is that now to get the values you'd call the functions instead of directly accessing the properties like so:

$deviceInfo = new DeviceInfo($cpe_mac);
$deviceInfo->get_c_devices(); // returns devices

For an example this simple, the extra code might not be worth it, but this does make it easier to update this code in the future without breaking all the points that these functions are called in the rest of your application.

like image 42
Joel Bell Avatar answered Nov 03 '22 04:11

Joel Bell