From 3780adf45a018dacd0c8fa48498a1a5db387dde3 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 21 May 2016 08:47:56 -0400 Subject: [PATCH] [Form] Making the name property private to be more realistic --- cookbook/form/form_collections.rst | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index 542a868c65f..840e1a5d9c8 100644 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -69,13 +69,18 @@ objects:: class Tag { - public $name; - } + private $name; -.. tip:: + public function getName() + { + return $this->name; + } - The ``name`` property is public here, but it can just as easily be protected - or private (but then it would need ``getName`` and ``setName`` methods). + public function setName($name) + { + $this->name = $name; + } + } Then, create a form class so that a ``Tag`` object can be modified by the user:: @@ -162,10 +167,10 @@ In your controller, you'll now initialize a new instance of ``TaskType``:: // dummy code - this is here just so that the Task has some tags // otherwise, this isn't an interesting example $tag1 = new Tag(); - $tag1->name = 'tag1'; + $tag1->setName('tag1'); $task->getTags()->add($tag1); $tag2 = new Tag(); - $tag2->name = 'tag2'; + $tag2->setName('tag2'); $task->getTags()->add($tag2); // end dummy code