Skip to content
Advertisement

How to create PDO connection in __construct()?

I have one class that is containing three methods. All of those methods need to database, Then I want to create a system for connection to database before everything. Something like this:

JavaScript

Now I want to know (in first) what I did is a standard approach? and (in second) how can I check [if connection was disconnect (in other word connection does not exist) then connect] ?

Advertisement

Answer

To access class properties, you have to use -> syntax:

JavaScript

If you want to detect errors, enable error signalling with

JavaScript

and use try/catch around the code that uses the DB:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement