same php include on multiple blocks

Permalink
I have developed a couple of custom blocks. They have a common include (rb.php which I like btw). The issue is that when I put the include in my custom code classes, I receive errors any time I try to add a new block or add functionality. This error is basically that I cannot redeclare a class in rb.php, which I assume is my duplicate include being attempted? I have also tried include_once

Current resolution: I can comment out the php include in either file. That particular block will not fuction but I can then do my editing via the dashboard. After edits are complete I uncomment and things seem to function in runtime correctly.

Questions:
Is it proper practice to have this type of include in a custom block or should I put this include somewhere at a 'higher' level?

Has anyone else noticed this behaviour and found a resolution?

I accept any and all input. I am willing to try anything so feel free to comment.

Thanks for any assistance!!

 
Remo replied on at Permalink Reply
Remo
It's a bit hard to know where to put your code without seeing your block, but in general, I'd suggest to put this stuff in the controller.

The easiest but imho ugliest way would be something like this:
if (!function_exists('my_function')) {
   function my_function() {
      ...
   }
}
JohntheFish replied on at Permalink Reply
JohntheFish
I generally use similar code, but at a class level.
So my shared code goes in a c5 library file that is copied into both block packages. The library then does the if exists test:
// in file /libraries/my_class.php
if (!class_exists('MyClass')){
class MyClass{
/*
Some functions shared between packages
*/
}
}
//
// Then to use it
Loader::library('my_class', 'my_package_handle');
//
// And in another package that also declares it
Loader::library('my_class', 'my_other_package_handle');