1

I would create two main classes in PHP5: 1- Database 2- Session These two classes will be used in a page "index.php" (and many others) in order to provide connection to the database and the use of session in future queries. I could simply make two objects and use them as in the following example:

require('Session.php');
require('Database.php');

$session = new Session();
$session->startSession();

$database = new Database();
$database->query();

But I don't like very much this solution. I would recall in my "index.php" just one class called Processor.php and recall both Session and Database classes too.

I would see this:

require('Processor.php');
$session = ...? call all methods of session
$session->
$database = ..? call all methods of database
$database->

How can I implement this idea? I would make that with Singleton pattern. Who can help me?

Andrea Limoli
  • 155
  • 1
  • 3
  • 12
  • The purpose of a Singleton is to ensure a class has only one instance, and provide a global point of access to it. You don't need either for what you describe. If no kittens die if a second instance is instantiated, you don't need to enforce uniqueness. Simply don't instantiate a second instance. And unless you are designing around legacy code you can easily circumvent the need for global access by using Dependency Injection. – Gordon Feb 05 '14 at 22:32

0 Answers0