Global variables question

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
KaileyT
Community Team Member
Community Team Member
Posts: 2933
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Global variables question

Post by KaileyT »

I have a few variables I need available in all of the PHP files in my extension. Unfortunately, the code is not public (yet), so I'll have to go with examples.

Code: Select all

$my_int_var = $this->request->variable('miv', 0);
$this->functions->get_data($this->user->data['user_id']);
Normally, I would change the function call to pass $my_int_var, but since that call is in another extension that's not an option. So, how do I make $my_int_var available should it be needed?
Kailey Truscott - Community Team
User avatar
david63
Registered User
Posts: 19048
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: Global variables question

Post by david63 »

One way, which may not be the best, that I have used is to write/read it as a dynamic config variable
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
KaileyT
Community Team Member
Community Team Member
Posts: 2933
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Global variables question

Post by KaileyT »

That might actually work. Thanks!
I wonder though - will that pass validation?


Actually, on second thought, maybe not. $my_int_var will be different depending on which page a user is visiting. It could be 1 for me, but 3 for you and 2 for some other user.
Kailey Truscott - Community Team
User avatar
david63
Registered User
Posts: 19048
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: Global variables question

Post by david63 »

David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
david63
Registered User
Posts: 19048
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: Global variables question

Post by david63 »

Further delving and it looks as if you may be able to use parameters

https://symfony.com/doc/4.1/service_con ... eters.html
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
KaileyT
Community Team Member
Community Team Member
Posts: 2933
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Global variables question

Post by KaileyT »

david63 wrote:
Sat Sep 26, 2020 1:34 pm
This any help?

http://www.dinduks.com/symfony-2-using- ... variables/
It helps for ½ of the problem. I still need to make them dynamic.

I'm not fully understanding the second link though. I'll have to do more reading.
Kailey Truscott - Community Team
User avatar
david63
Registered User
Posts: 19048
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: Global variables question

Post by david63 »

kinerity wrote:
Sat Sep 26, 2020 2:09 pm
I'm not fully understanding the second link though.
The way that it reads to me is that to make "dynamic" you set the parameter
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
KaileyT
Community Team Member
Community Team Member
Posts: 2933
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Global variables question

Post by KaileyT »

Thanks for the pointers. I'll see what I can do to make it work using that information.
Kailey Truscott - Community Team
rxu
Extensions Development Team
Posts: 3467
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Global variables question

Post by rxu »

So, to make it clear: do you want to pass $my_int_var from one extension to another? if so, will those extensions share the same runtime or is it like ext1 gets it at 1 page call and then ext2 gets it at another page call?
User avatar
KaileyT
Community Team Member
Community Team Member
Posts: 2933
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Global variables question

Post by KaileyT »

rxu wrote:
Sat Sep 26, 2020 3:43 pm
So, to make it clear: do you want to pass $my_int_var from one extension to another?
Yes, but even forgoing that function, I'd also like to pass it to other functions in my extension without have to explicitly add it to all my functions.
Kailey Truscott - Community Team
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4741
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Global variables question

Post by kasimi »

If you need the value across page loads, store it in the users table. If you need it only during a single page load, store it in a service that you then inject where you need it.
Post Reply

Return to “Extension Writers Discussion”