Jump to content

OOP and accessing variables


Dragen

Recommended Posts

Hi,

I'm sure this has been asked before, but couldn't see anything.

 

I've created a class which stores my mysql details, connects and carries out queries (select, insert, delete, update).

I'm calling it like so:

$database = new databse;

on a page which is included on every other page.

What is the best way of accessing the connection from within a second class?

Should I use:

global $database

(I'm assuming not as that negates the use of OOP, making it dependant on a variable not within it's own protocol.

 

Should I then pass the connection variable to the class like so:

someClass{
public someClass($db){
	//use connection here
}
}

 

 

Or should I not even call the database class in the first place and instead simply create a new instance of the class whenever needed, closing the connection each time?

I figured this idea was also un-productive as far as OOP is concerned as it requires the need to know the details of the database class and cannot, therefore be ported to another program.

 

Any ideas?

I'm sure it's probably rather simple :)

Link to comment
https://forums.phpfreaks.com/topic/158172-oop-and-accessing-variables/
Share on other sites

Well you'll obviously need to connect to the database somehow.

 

Personally, if I know there is a connection to my database already (I also have a class to connect, query etc...) I will just global the connection, if not create it.

 

If I am unsure, I will use isset() to determine whether or not there is a connection.

 

Creating new connections to run concurrently with ones that already exist seems pointless to me. You're just making multiple connections for no reason, thus will slow down your script.

 

Up to you though...

It's a matter of preference.  You can use a singleton for the connection or just a regular class that you use to open and close the connection.  The only reason I mention the singleton is it's was my first "aha!" moment with design patterns, and it's really easy to implement.  Opinions may differ...

 

The following tutorial is available at:

 

http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html

 

I changed it to reflect a connection instead of a query.  Notice the constructor is private so it cannot be accesses outside the class.

 

<?php
// Example of a Singleton Database class
class Database
{
// Store the single instance of Database
private static $m_pInstance;

// Private constructor to limit object instantiation to within the class
private function __construct() 
{ 
	echo "Constructor called<br />\n"; 
}

// Getter method for creating/returning the single instance of this class
public static function getInstance()
{
	if (!self::$m_pInstance)
	{
		self::$m_pInstance = new Database();
	}

	return self::$m_pInstance;
}

// Test function to simulate a connection
public function connection()
{
	echo "Running query on database conenction...<br />\n";
}

}

// Wrap the test code in a function 
function testFunction()
{
// Get the single instance of the Database class using the gettor 
// method we created.  Then call it's query method to output some text
$pDatabase = Database::getInstance();
$pDatabase->connection();
}

// Start the test
testFunction();
testFunction();

// After running this script you will see that the constructor was only called
// once, showing that only one instance of the class was created.

?>

 

 

 

 

Thanks, that's what I've done in the past, just global the variable.

I was just trying to think of it from a purely OOP view.

It means that I would not then be able to simply take out the class and dump it into another project and have it work, which is an ideal of OOP.

That's why I had the idea of passing the connection variable via reference..

 

 

Andy, thanks for that. It looks good, I haven't come across singletons before, being rather new to OOP..

Mind my ignorance though, but is that not really the same as using a normal class call within another class?

It would still mean creating e new reference to the class on each call, unless I am mistaken.

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.