Skip to content

Property access #7870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions components/property_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,31 @@ can use setters, the magic ``__set()`` method or properties to set values::
$this->lastName = $name;
}

public function getLastName()
{
return $this->lastName;
}

public function getChildren()
{
return $this->children;
}

public function __set($property, $value)
{
$this->$property = $value;
}

// ...
}

$person = new Person();

$accessor->setValue($person, 'firstName', 'Wouter');
$accessor->setValue($person, 'lastName', 'de Jong');
$accessor->setValue($person, 'children', array(new Person()));
$accessor->setValue($person, 'lastName', 'de Jong'); // setLastName is called
$accessor->setValue($person, 'children', array(new Person())); // __set is called

var_dump($person->firstName); // 'Wouter'
var_dump($person->getLastName()); // 'de Jong'
var_dump($person->children); // array(Person());
var_dump($person->getChildren()); // array(Person());

You can also use ``__call()`` to set values but you need to enable the feature,
see `Enable other Features`_.
Expand Down
2 changes: 1 addition & 1 deletion service_container/service_decoration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@ order by configuring the priority of decoration, this can be any integer number

The generated code will be the following::

$this->services['foo'] = new Baz(new Bar(new Foo())));
$this->services['foo'] = new Baz(new Bar(new Foo()));

.. _decorator pattern: https://en.wikipedia.org/wiki/Decorator_pattern