Jump to content

Declaring new


bdmovies

Recommended Posts

I am new to the OO ideology...

 

I've got my database.class.php which has everything I need to interact w/ MySQL.

 

On my pages, why would I ever do anything other than $db = new database? Is there ever a situation where I would need to call multiple new databases on 1 page?

 

Also, what is the syntax to access properties from my page? $case->getCaseInfo($caseID) fires the getCaseInfo function, but how could I get the the case name, which would be a property?

Link to comment
https://forums.phpfreaks.com/topic/137855-declaring-new/
Share on other sites

Assuming the database class you have connects to only one database then no, I can't see a situation where you would want to create multiple instances of the database class.

 

Once the class is instantiated (once) any part of your script should be able to use it to access the database.

 

$class->property = 'some value';

Link to comment
https://forums.phpfreaks.com/topic/137855-declaring-new/#findComment-720480
Share on other sites

I have a db class that holds the login credentials for about 5-6 databases. I put this file in my hosting root (outside of my web root) and use the 1 file to handle database communication for all the sites. There have been times that I needed to instantiate more than 1 database connection on a page.

 

I pass a short name for each connection into the class. e.g.

$db = new cDatabase('blog') or $db = new cDatabase('portfolio');

 

So yes there could be times you need to initiate more than 1 db connection. Though not often, and it depends on whether or not your db class is set up to connect to multiple databases.

 

Nate

Link to comment
https://forums.phpfreaks.com/topic/137855-declaring-new/#findComment-720554
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.