Skip to content
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

updated observer pattern example and docs #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ Wikipedia says

**Programmatic example**

Translating our example from above. First of all we have job seekers that need to be notified for a job posting
Translating our example from above. First of all we have job seekers that need to be notified for a job posting.
```php
class JobPost
{
Expand Down Expand Up @@ -1837,15 +1837,17 @@ class JobSeeker implements Observer
}
}
```
Then we have our job postings to which the job seekers will subscribe
Then we have our job postings to which the job seekers will subscribe. When notifying observers there are chances that Job Seeker might add another observer to observers list. To avoid exponential growth of observers list & to avoid cyclic dependency, it is always good to have copy of observers before notifying.
```php
class EmploymentAgency implements Observable
{
protected $observers = [];

protected function notify(JobPost $jobPosting)
{
foreach ($this->observers as $observer) {
{
$observersCopy = $observers->getArrayCopy();

foreach ($observersCopy as $observer) {
$observer->onJobPosted($jobPosting);
}
}
Expand Down