Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Global functions to access global data

Hello,

I have to access some global data, for example session('company_id') which is the company for which the user wants to connect to the app (a user can work for several companies using the same app).

Is it possible to access easily these global data : company_id, company, ... from global functions accessible anywhere in the code ?

function getCurrentCompany()
{
}

function getCurrentCompanyId()
{
}

And other needs like knowing for example if the connected user is admin or only trainer for the current company.

function isAdmin($company_id)
{
}

Creating a new helper could be a solution ? Or a service ?

Thanks for your help.

V

0 likes
3 replies
ramonrietdijk's avatar
Level 30

You could create a helpers.php file where you can define these functions. Make sure to include it in your composer.json.

"autoload": {
    "files": [
        "app/helpers.php"
    ]
}

Then you should be able to use them everywhere within your application.

I personally prefer to use service classes because of dependency injection.

1 like
ramonrietdijk's avatar

@vincent15000 In your case I think it's fine to stick to helper functions. I often work with some dependencies and using service classes makes it a lot easier. Plus it can be helpful splitting code across multiple classes if a lot of functions are needed.

1 like

Please or to participate in this conversation.