Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are talking about the <a href="http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Request.html" rel="nofollow">Request</a> and <a href="http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Response.html" rel="nofollow">Response</a> objects, there was a discussion about this on the Symfony developers mailing list a few days ago. I invite you to take a look at it <a href="https://groups.google.com/forum/#!topic/symfony-devs/CiJuSno85_8" rel="nofollow">here</a>.</p> <p>Why not getters? Not sure if there is a definitive answer to this but I think it is a decision based on personal tastes mainly.</p> <p>Does it break encapsulation? Not really in my opinion for this particular case. My reasoning is that for now, no special logic is performed on the various objects that are public right now. So in the end, you would end up retrieving the object via a getter and read or modify it directly. There is not much difference with retrieving the object using a public property.</p> <pre><code>// With Getters $parameterBag = $request-&gt;getQuery(); $parameterBag-&gt;get('key'); // With Public Properties $parameterBag = $request-&gt;query; $parameterBag-&gt;get('key'); </code></pre> <p>Encapsulation should be enforced when you need to be sure that a property has a particular value or format. For example, say you have a class with a cost property and this property should never be negative. So if the cost property was public, it could be possible to set it to a negative value by doing something like <code>$receipt-&gt;cost = -1;</code>. However, if you make it private and the user of the class is only able to set it via a setter, then you could ensure that the cost is never below 0 by doing some special validation in the setter code.</p> <p>In our case, we are talking about a collection object, a ParameterBag object to be precise. I don't think there are special requirements on this object but I could be wrong. So for me, it is correct to have access to those properties via public properties.</p> <p>The main argument I could see in favor of the getters is that it would be more consistent with the other parts of the framework where getters are used. However, the getters could co-exist with the public properties.</p> <p>To conclude, I think it is safe for this particular case. Public properties should be used only in special cases where it seems to be beneficial and where it is correct to do so.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload