Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In symfony, what is a bag?

Tags:

oop

php

symfony

I've found a decent answer regarding what is a bag in the context of java: What is meant by the term "bag"?

I did a google search and found that Symfony provides a lot of different "bag" interfaces like this one:

http://api.symfony.com/2.3/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.html

In symfony, is the term bag meant to be interpreted as the same way? Also, how is this different from a "container" or "collection".

like image 575
chrisjlee Avatar asked Nov 05 '13 20:11

chrisjlee


2 Answers

A bag (the object's name is ParameterBag) contains variables or parameters.

A collection contains a list of elements (think of it as an oriented-object version of array with a lot of useful methods to manipulate the values). The object comes from the Doctrine framework. Symfony also has a concept of Collection and it is explained in its documentation.

A Container contains all kind of objects, it has its own ParameterBag object with its parameters loaded from anything within the parameters key of your config file.

A Request object ($this->container->get('request')) also has its own ParameterBag.

Code-wise, the Container's parameter bag and the Request's parameter bag are different and could have different methods but their goals are identical.

like image 67
Thomas Potaire Avatar answered Sep 26 '22 00:09

Thomas Potaire


A bag is short for ParameterBag. A bag is a datatype used to help store data, similar to a collection in java. A bag specifically can access values via key/value as Dagon pointed out, but also has other operations that are useful.

The public operations of a bag are: clear, add, all, get, set, has, remove, resolve, resolveValue, resolveString, isResolved, escapeValue, and unescapedValue.

If you'd like to see the operations of a bag in detail, you can do so here: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

like image 43
James Oravec Avatar answered Sep 24 '22 00:09

James Oravec