Skip to content

Commit 7afa14f

Browse files
iulyanpxabbuh
authored andcommitted
Property access
1 parent 75623c4 commit 7afa14f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

components/property_access.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,31 @@ can use setters, the magic ``__set()`` method or properties to set values::
259259
$this->lastName = $name;
260260
}
261261

262+
public function getLastName()
263+
{
264+
return $this->lastName;
265+
}
266+
267+
public function getChildren()
268+
{
269+
return $this->children;
270+
}
271+
262272
public function __set($property, $value)
263273
{
264274
$this->$property = $value;
265275
}
266-
267-
// ...
268276
}
269277

270278
$person = new Person();
271279

272280
$accessor->setValue($person, 'firstName', 'Wouter');
273-
$accessor->setValue($person, 'lastName', 'de Jong');
274-
$accessor->setValue($person, 'children', array(new Person()));
281+
$accessor->setValue($person, 'lastName', 'de Jong'); // setLastName is called
282+
$accessor->setValue($person, 'children', array(new Person())); // __set is called
275283

276284
var_dump($person->firstName); // 'Wouter'
277285
var_dump($person->getLastName()); // 'de Jong'
278-
var_dump($person->children); // array(Person());
286+
var_dump($person->getChildren()); // array(Person());
279287

280288
You can also use ``__call()`` to set values but you need to enable the feature,
281289
see `Enable other Features`_.

0 commit comments

Comments
 (0)