Reputation: 58652
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.
I've found get_defined_vars, but that is not really what I am looking for.
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?
Upvotes: 5
Views: 972
Reputation: 8214
Use pass-by-reference.
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!
Also, depending on your use case, if your PHP code isn't deployed from source to the server directly (e.g. if you released packaged phars, etc.), you may want to use cpp
(the C preprocessor) to preprocess your files.
Upvotes: 2
Reputation: 2718
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.
Upvotes: 4
Reputation: 35180
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!
Upvotes: 4
Reputation: 1458
//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');
Upvotes: 1
Reputation: 68
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
.
Upvotes: 4