Jump to content

Context (no more globals !)


Recommended Posts

I would like to introduce a new technical feature of PrestaShop 1.5, which was developped to match 2 goals :

  • Stop using globals
  • To change context in some methods

The Context is a light implementation of Registry pattern, a classe where data like customer, cookie, cart are stored.

 

 

You can access to context in front and back controllers this way : $this->context.

Or you can use everywhere : Context::getContext().

 

For example, do not use :

global $cookie;
$language_id = $cookie->language_id;

but use

$language_id = $this->context->language->id;

 

Retrocompatibility with globals will be maintained during 1.5 but removed in 1.6, so you will have to take care of good practicies in your plugins :)

 

You can discuss of this technical feature here, and give us feedbacks.

Link to comment
Share on other sites

Context class is great news. Thanks for introducing it, the code will look much leaner and cleaner w/o the use of globals.

 

Are you planning to introduce an adapter to 1.4 before its EOL so the plugins using new context concept will not have to do things like

 

if(1.4) use globals
if(1.5) use context

Link to comment
Share on other sites

  • 4 weeks later...

Sorry for the spam, but I thinked it twice about Registry :rolleyes:

 

Raphaël > Actually, I think your example is not good !

 

You show how the $cookie variable can be found in the Registry... But IMHO this is not the way it should be retrieved.

 

Registry can also have drawbacks : we shouldn't rely on Registry as an "all-purpose" object storage.

I mean, not everything should be stored inside the Registry.

 

We should have a registry to store constants and general settings shared among the application, and singletons for objects that are unique within the application.

 

Starting from your example, I would have imagined something like :

 

$themeDir = Registry :: getInstance()->get("THEME_DIR");
$cookie = Cookie :: getInstance();

Link to comment
Share on other sites

Are you planning to introduce an adapter to 1.4 before its EOL so the plugins using new context concept will not have to do things like

 

if(1.4) use globals
if(1.5) use context

 

Yes, there will be an adapter to use the context in modules with PrestaShop 1.4.

 

Why didn't you call the class Registry then ? It may seems trivial, but Context involves another concept...

Because we didn't want to hear "It's not really a Registry, why did you name it like this!!!" :)

 

You show how the $cookie variable can be found in the Registry... But IMHO this is not the way it should be retrieved.

In Raphael's exemple you can see that you don't access the cookie anymore, but the language object. You don't have to care if the language was extracted from the cookie, the database, etc.

 

We wanted to store the objects in the context because they may need complex initialization, which is not possible with static singleton calling, and because each object was often created and destroyed more than once during a page execution.

Link to comment
Share on other sites

  • 6 months later...

Does this mean I can use a 1.5 module on my 1.4.4.1? If so any news on when this adapter is coming?

 

Actualy, you can uses globals in 1.4 and globals or context in 1.5. Context is highly recomended in 1.5... Because globals just disappear in 1.6 !

 

If your 1.5 module only uses the context, you can't install this in your 1.4 installation. But, if your 1.5 module uses globals AND context (condition on the PS version), so you can.

Link to comment
Share on other sites

×
×
  • Create New...