Jump to content

Recommended Posts

Hi everyone.

 

I have a few years of programming experience (personal projects and such), but I'm relatively new to web programming, I'm in the process of learning PHP and with a C++ background this is going fairly well, but I'm having problems with a few concepts.

 

The first thing I don't know how to implement, or if I should implement it this way, is a database connection class I'm working on. Basically, I wan't to access to the database through this class from many other different scripts by declaring an object on said script like so

 

$db = new Database("server","db","user","psswd")

$db->connect();

 

But I don't want to put the server info, db and all that on each script at the object declaration, because it will change. Is it ok to put this info in a text file and have class or whatever read the connection info from there to use it across the scripts? I'm thinking about some sort of ini or xml config file where settings like this could be stored, but I don't know what's the common/standard aproach in a web envioroment.

 

Thank you.

Just do an include of the db script and call the variable $db.

 



[quote author=Hioushi link=topic=297073.msg1406980#msg1406980 date=1273153344]
Hi everyone.

I have a few years of programming experience (personal projects and such), but I'm relatively new to web programming, I'm in the process of learning PHP and with a C++ background this is going fairly well, but I'm having problems with a few concepts.

The first thing I don't know how to implement, or if I should implement it this way, is a database connection class I'm working on. Basically, I wan't to access to the database through this class from many other different scripts by declaring an object on said script like so

$db = new Database("server","db","user","psswd")
$db->connect();

But I don't want to put the server info, db and all that on each script at the object declaration, because it will change. Is it ok to put this info in a text file and have class or whatever read the connection info from there to use it across the scripts? I'm thinking about some sort of ini or xml config file where settings like this could be stored, but I don't know what's the common/standard aproach in a web envioroment.

Thank you.
[/quote]

Yes, but I'd like to know if it's ok for the Database class to retrieve the connection details like server, database and user from a xml config or ini file so that I can separate the database class implementation from the actual connection parameters.

It would be customary to put all application configuration settings in to a config.php (or similar name) file and include that at the beginning of an application. By putting php code (variables or defined constants) into a .php file, it is automatically protected against revealing the values should someone discover or guess the file name and request the file. If you use a .xml or .ini file, you must take additional steps to protect the contents.

 

Once the variables or defined constants have been included, you just use them in the $db = new Database($server,$db,$user,$psswd) function call.

 

Also, keep the class general purpose (pass the values in as parameters) so that it would be possible to create multiple instances of the class should you need to access more than one database in an application.

Thank you, that's what I was thinking about, leaving the class general purpose.

 

Now, if I declare the connection parameters in the config.php file, like $server,$db,$user,$psswd, Shouldn't this variables need to be global in order to be available to other scripts?

available to other scripts

 

What other scripts?

 

You would include the config.php file at the start of any web page that needs to use the configuration settings. Including a .php file is similar to including a .h file in C. You are including variables, defined constants, function definitions, and class definitions that are needed by the rest of the code on the page. A php include statement is essentially the same as copy/pasting the contents of the file being included into the source where the include statement is at and the contents exist in the same scope where the include statement is at.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.