Patch #4335

Rewrite driver based classes

Added by Dmitry T. 6 months ago. Updated 6 months ago.

Status:Closed Start date:11/16/2011
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:Core
Target version:v3.3.0
Resolution:invalid Points:

Description

I'll try to explain. We can't have more than one instance of a class of the same type at the moment. Let's take the Cache class. Assume I want to use memcache based cache. Ok, I write Cache::instance('memcache') and everything seems fine. But then I need the second cache with another parameters (other server etc). How should I do that? I must create another empty class (for example, class Cache_Memcache2 extends Cache_Memcache), add a new config and only then I can use Cache::instance('memcache2'). IMO it itsn't a right way to to this.

Instead of doing so I suggest to move the name of a driver class to config. For example, config/cache.php:

return array(
  'memcache2' => array(
    'driver' => 'memcache',
    // other options
  ),
);

Then we can just add a new config and instantiate a class with other config - Cache::instance('memcache2') without a need of creating a dummy class

History

Updated by Dmitry T. 6 months ago

Generally speaking, for session library, just one line (51) in session.php file should be changed from

$class = 'Session_'.ucfirst($type);
to
$class = 'Session_'.ucfirst($config['driver']);

And "driver" parameter should be added to config files of course

Updated by Jeremy Bush 6 months ago

This is basically how the database class works.

Updated by Isaiah DeRose-Wilson 6 months ago

Yeah that makes sense for a lot of the classes. I'm not sure Session is a good example though. When would you ever have multiple session instances?

Updated by Dmitry T. 6 months ago

Jeremy, you are right, Cache and Database already work this way, just Session doesn't

Updated by Isaiah DeRose-Wilson 6 months ago

When/why would you need a second instance of the Session class with a different config?

Updated by Dmitry T. 6 months ago

I don't know :)
Actually I was writing a custom session class and when I saw how session class is created I thought that cache and database work the same way. But then (after I've created this issue) I've lookes into the code and understood that only session works like this. And I felt much better then ;)

Updated by Isaiah DeRose-Wilson 6 months ago

  • Status changed from New to Closed
  • Resolution set to invalid
  • Points deleted (5)

Also available in: Atom PDF