Skip to content
Open
Changes from 1 commit
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 ($this->observers as $observersCopy) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was new to php , mistakenly added loop logic. updated same in next revision.

$observer->onJobPosted($jobPosting);
}
}
Expand Down