Error when I have multiple instances of a block on a single page

Permalink
I'm developing a weather block and it is working perfectly well. Except if I have more than one instance of the block on a single page. Then I get "Whoops \ Exception \ ErrorException (E_ERROR)".

The exception always rises at the last function of my "view.php". I've tried to change the order of the functions and the party always stops at the last function regardless.

Please point me in the right direction – it's probably some total rookie error since this is my first attempt to develop a block.

The block is here:https://github.com/o3jvind/cool_weather...

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
The functions declared in the view will be polluting the global namespace. When you have more than one block on the page, they end up getting declared twice.
Solutions:

General: Never declare functions outside of a class

Then
a) Move these functions into the block controller, call them from the view as
$controller->methodName()

or

b) Move the functions into a separate class, instantiate that in the view and call them on the instance.
O3JVIND replied on at Permalink Reply
BIG THANX

Moved the functions to the controller.

I have a working block now :-)

https://github.com/o3jvind/cool_weather...