Global functions?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm
Contact:

Global functions?

Post by vipaka »

Hopefully a quick and easy question.

How do we build custom global functions correctly into the PHPBB ext system? I have a custom PHP function I want available on every page, and without manually writing it into functions.php or one of the other global php files I am not sure how to achieve that. This is an entirely custom function (and I am amenable to having it injected as its own custom class of custom functions globally as well, and then sending that out as a new global variable or something) that can't conceivably hook into any existing events we have available, but it definitely needs to be globally available on every page without necessarily getting-called (at least, not immediately) on every page. I'm picturing it functioning like most of our global variables already do, where you either call them with global $var or you add them to the constructor of a class, and then call them off $this->.

I tried setting up a service and listener with the functions just kind of dumped inside the load language function there but no luck (it was a shot in the dark).

For example purposes, pretend it reads like below.

Code: Select all

public function do_this_thing_on_every_page(){
     //query to insert stuff into db or some such
}
Use case:

Code: Select all

      if (user takes some action they could take anywhere at any time){
            do_this_thing_on_every_page();
      }
      
      OR
      
      global $newThing;
      if (conditional){
          $newThing->do_this_thing_on_every_page();
      }
Thanks in advance!
Last edited by vipaka on Wed Aug 22, 2018 2:36 am, edited 2 times in total.
Curious about my work? See it for yourself.
Image
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: Global functions?

Post by GanstaZ »

Best thing to do, is to read about extensions & everything that is written in phpbb docs. For making something global you can try core.page_header event or for some functions/methods core.common event can be used as well.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm
Contact:

Re: Global functions?

Post by vipaka »

Full answer for anyone else who might need want it (I found it in another ext I had already written).

in event listener.

Code: Select all

return array(
     			 'core.common'            => whatever',
     )

Code: Select all

 public function whatever($event)
  {
    global $phpbb_root_path, $phpEx;

    include($phpbb_root_path . 'ext/extAuthor/extName/includes/functions_whatever.' . $phpEx);
  }
Add your functions to the path in the include statement.
Curious about my work? See it for yourself.
Image
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: [SOLVED] Global functions?

Post by 3Di »

That's not the right way to do it if I can say.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm
Contact:

Re: [SOLVED] Global functions?

Post by vipaka »

3Di wrote: Mon Aug 20, 2018 11:49 pm That's not the right way to do it if I can say.
What way do you prefer?
Curious about my work? See it for yourself.
Image
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [SOLVED] Global functions?

Post by martti »

From your description I think you just need to listen to a phpBB PHP event that is triggered on every page (like i.e core.page_header), not a global function.

Global functions go against the philosophy of extensions not being global in scope.
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm
Contact:

Re: [SOLVED] Global functions?

Post by vipaka »

martti wrote: Tue Aug 21, 2018 6:32 am From your description I think you just need to listen to a phpBB PHP event that is triggered on every page (like i.e core.page_header), not a global function.

Global functions go against the philosophy of extensions not being global in scope.
This extension needs to be global. It needs to be accessible from within other extensions (and from within core phpbb). I know that's not the greatest situation (and not super modular like most extensions should be) but its necessary for it to work how I need it to without having to duplicate and maintain dozens of lines of codes dozens of times.
Curious about my work? See it for yourself.
Image
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: [SOLVED] Global functions?

Post by 3Di »

As it is this code wil not pass the validation (I am quite sure).
On a side note can't be considered as "solved" since is leading the future readers/extension writers to wrong directives.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm
Contact:

Re: [SOLVED] Global functions?

Post by vipaka »

3Di wrote: Wed Aug 22, 2018 12:59 am As it is this code wil not pass the validation (I am quite sure).
On a side note can't be considered as "solved" since is leading the future readers/extension writers to wrong directives.
Not every extension is written with the intent of passing the validation process. I have at this point probably written 20+ extensions but they are not open source/written with the purpose of passing validation (although I do try to use the standards whenever I can, because its stupid to work against yourself when there is an easier and better method readily available). I will remove the [SOLVED] from the topic heading.

That said, I have seen zero *actual* help emerge from this topic, and its the second time I've raised the issue of writing things with a global effect (there was a post a few months back, where I originally came up with the solution above, which I bumped several times with progress I'd made on the topic and no illuminating replies on how to actually do it). I have, however, seen people telling me I'm wrong, they're right, they know a way (but don't share), to go read the documentation (which I have) and being generally unhelpful, and frankly--rude. If there isn't an approved solution to this problem, that's an issue with the phpbb framework's usability and flexibility for ext authors like myself. If the ext system disallows extensions from directly affecting one another, that is a completely acceptable architectural choice, but drop that kernal somewhere in your documentation instead of simply extolling the virtues of ext secularization (and omitting the drawbacks/missing functionalities).

On top of that, responses like above to topics like this are incredibly unfriendly to developers who are choosing to try and work within your framework. PHPBB is not the only forum software on the internet, and you do yourselves a disservice to your community by acting dismissive and arrogant when people are asking for your help in the correct forum, about real issues with writing exts, using your own framework's terminology. There were several reasons I stopped writing open source extensions for phpbb (or posting here at all), and I remember a lot of why that is now.
</rant>
Curious about my work? See it for yourself.
Image
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Global functions?

Post by 3Di »

Honestly, it's far from my way of life being rough but it's not the first time I've been accused of being one, I impute this side effect to the fact that I'm not a native speaker. Anyone who knows me personally could say that I am not a rude person at all.

Returning to the point about this topic, I have only seen a request that is not very clear, I can see the solution, of course, but there are several ways to solve an issue and if I am not sure what you want to do I can not help you. never thought about sharing your code so can be "read"?

Over and above that you have declared yourself solved the problem by posting a solution that does not follow the guidelines and the concept itself and does not help. Not to mention the fact that you do not intend to make your work public but you ask for help: can be.. but please don't pretend that everyone is here to guess your quizzes. You are sticky with your ideas, so am I with mines.

Moreover,
vipaka wrote: Wed Aug 22, 2018 2:36 am If the ext system disallows extensions from directly affecting one another,
that's completely un-true.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
Ozo
Registered User
Posts: 330
Joined: Mon Dec 13, 2010 7:57 pm

Re: Global functions?

Post by Ozo »

This person seeks help with closed source code on phpbb.com...
User avatar
Ger
Registered User
Posts: 2108
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: Global functions?

Post by Ger »

vipaka wrote: Wed Aug 22, 2018 12:28 am This extension needs to be global. It needs to be accessible from within other extensions (and from within core phpbb). I know that's not the greatest situation (and not super modular like most extensions should be) but its necessary for it to work how I need it to without having to duplicate and maintain dozens of lines of codes dozens of times.
If setup properly, extensions can just hook into each other. For example, I have created a simple extension that makes Feed Post Bot create articles in cmBB. It's the essence of dependency injection: inject one service into another and off you go.
My extensions:
Simple CMS, Feed post bot, Avatar Resize, Modbreak, Magic OGP, Live topic update, Modern Quote, Quoted Where (GDPR) and Autoresponder.
Newest: FAQ manager for 3.2

Like my work? Buy me a coffee to keep it coming. :ugeek:

-Don't PM me for support-
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [SOLVED] Global functions?

Post by martti »

vipaka wrote: Wed Aug 22, 2018 12:28 am It needs to be accessible from within other extensions (and from within core phpbb). I know that's not the greatest situation (and not super modular like most extensions should be) but its necessary for it to work how I need it to without having to duplicate and maintain dozens of lines of codes dozens of times.
Don't forget your extension can also emit it's own phpBB PHP events that other extensions can listen to.
Post Reply

Return to “Extension Writers Discussion”