I have to share an instance of a config object in PHP.
This object contains db username and db password. The db user can change. So I have to share te object instance.
Example.
Default db user is ‘default’. default has only rights to SELECT data from db.
When website user logges in on website as admin, db user has to change to ‘admin’. admin has rights to SELECT, UPDATE, DELETE etc.
When web user logges in as a normal user, db user has to change to ‘normal’ normal has rights as SELECT, UPDATE etc.
I’ve to share the config instance. What is the safest way to share this object?
You could make use of the singleton pattern in order to keep one instance accessible from anywhere within your application.
I also suggest you have a singleton as a connection-handler where you can get, set and maintain connections, in which you also store the login credentials.
Example: