Jump to content

Recommended Posts

I'm currently working on a project to convert a procedural based application into objects. my overall design is basically complete but I'm having problems with one design issue. I have a database object that I create at every page load. I would like to be able to reuse this object in other classes whenever necessary. What would be the best way to do this? I've tried a few different things but they turn into spaghetti mess very quickly. 

Link to comment
https://forums.phpfreaks.com/topic/85897-object-design-releated-issue/
Share on other sites

I don't think there is a way to maintain state of the objects. HTTP Requests by definition are stateless.

 

On second thought, I wonder if it would be possible to serialize your object into a cookie :)  But that would raise all sorts of problems, like security issues, what happens if the client doesn't have cookies enabled, etc...

 

Out of curiosity, what were your previous attempts at this?

It's not possible to serialize a resource.

 

And, I don't know if DyslexicDog (genius name... wish I was creative haha) meant retain the object through multiple pages.

 

I think he was looking for something like this:

 


class Database {

}

class OtherClass {
     private $db = false;
     function __construct(&$db) {
          $this->db = $db;
     }
}

class OtherOtherClass {
     private $db = false;
     function __construct(&$db) {
          $this->db = $db;
     }
}

$db = new Database;
$otherclass = new OtherClass($db);
$otherotherclass = new OtherOtherClass($db);

 

 

Or....  You could do it this way if you want a more lazy approach:

 


class Database {

}

GetDatabase()->SomeMethod();

//This might require PHP 5... not sure.

function GetDatabase() {
     static $db = false;
     if($db === false) {
          $db = new Database();
     }
     return $db;
}

 

You could also have a main class that spawns the sub classes and so on....

The second approach is better than the first one however do something like this

/**
* @desc returns the instance of the object
*
* @author Ryan Zec
*
* @param void
* @return void
*/
public static function get_instance()
{
$configuration = configuration::get_instance();
if(!isset(self::$instance))
{
	self::$instance = new database($configuration->get_sql_host(),
						   $configuration->get_sql_username(),
						   $configuration->get_sql_password(),
						   $configuration->get_sql_database(),
						   $configuration->get_sql_type());
}
return self::$instance;
}

 

and then in each class

 

$db = database::get_instance();

 

This allows you do get the same database object(making it a singleton) and you don't have to worry about passing the object by reference every time you create a new object.

Instead of creating a lot of singletons you might want to make a registry instead. I believe the resources topic in the app design forum has some links regarding that. Otherwise you could just google it. You should still try to pass an object as an argument if possible though.

  • 3 weeks later...

Instead of creating a lot of singletons you might want to make a registry instead. I believe the resources topic in the app design forum has some links regarding that. Otherwise you could just google it. You should still try to pass an object as an argument if possible though.

 

Sorry for the delay I basically fell off the earth for the last couple weeks.

 

I like the singleton design idea, Why do you recommend a registry over a singleton and passing the database object overall?

 

 

'

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.