Split module PHP code into separate include files

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

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 are in my 10 controllers. If I need to fix something, I need to fix in 10 places.

I am looking for the best way to control this.


I thought to write 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 this function: $devices = get_device_info($cpe_mac);

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

I will not have access to all of my 5 variables in this function.

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


I found get_defined_vars , but that is not quite what I am looking for.


Question

How to implement it and implement it?

How to move a block of code and turn it back on?

Should I start viewing PHP Require / Include?


I open any offers at this moment.

Any tips / suggestions / help on this would be greatly appreciated!

+5
source share
5 answers

I do this all the time, especially for using the same header on the site. Just place this line in the document in which you want to request the code.

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

If you want to call the same block of code several times on the same page, change require_once to require .

+4
source

You can wrap everything in the DeviceInfo class, and then just use the properties of this 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 a class in your own file called DeviceInfo.php, and then where you need it, just

 include_once("DeviceInfo.php"); 

at the top of the file and create a new instance of this class. (I use include_once to make sure the DeviceInfo class is not overridden if it is already defined)

 $deviceInfo = new DeviceInfo($cpe_mac); 

You can access values ​​by accessing such properties.

 $deviceInfo->c_devices; 

This way you get code completion for the values ​​(depending on your IDE) and should not rely on remembering the key names of the array when you really want to use this information in your code.

If you want to take it one step further, you can even add getter functions to this class, so if you need to change how these values ​​are computed or retrieved without changing the API in the future, it's a lot easier. It will 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 is that now to get the values ​​that you would call the function instead of directly accessing these properties:

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

For example, it's simple, additional code may not be worth it, but it makes it easier to update this code in the future without breaking all the points that these functions call the rest of your application.

+4
source

If you do not want to change any variables on your page, and you are potentially thinking about using a global function, you can:

 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 call:

 extract(get_device_info($someVar)); 

and you will have access to:

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

As you always did

Please note: I am not saying this is a better answer than the rest, I am just saying this as an alternative.

Hope this helps!

+4
source

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 is not deployed directly from the source to the server (for example, if you issued fax packages, etc.), you can use cpp (C preprocessor) to preprocess your files .

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

Source: https://habr.com/ru/post/1259049/


All Articles