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

Add detachObserver #11037

Merged
merged 4 commits into from
Oct 29, 2016
Merged
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
20 changes: 19 additions & 1 deletion libraries/joomla/observer/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(JObservableInterface $observable)

/**
* Adds an observer to the JObservableInterface instance updated by this
* This method can be called fron JObservableInterface::attachObserver
* This method can be called from JObservableInterface::attachObserver
*
* @param JObserverInterface $observer The observer object
*
Expand All @@ -59,6 +59,24 @@ public function attachObserver(JObserverInterface $observer)
$this->observers[get_class($observer)] = $observer;
}

/**
* Removes an observer from the JObservableInterface instance updated by this
* This method can be called from JObservableInterface::attachObserver
*
* @param String $observer The observer class name
*
* @return void
*
* @since 3.6.0
*/
public function detachObserver($observer)
{
if (isset($this->observers[$observer]))
Copy link

Choose a reason for hiding this comment

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

You don't need to check that a variable is set before you unset it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, AFAIK unsetting not set index will give you index not exists notice.
So why would you trigger notices if you could avoid it? )

Copy link

@ghost ghost Jul 14, 2016

Choose a reason for hiding this comment

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

In this case you would get a Notice: Undefined variable: $this->observers if $this->observers would not be defined. But it's always defined (array).
protected $observers = array();
A not existing index doesn't throw a Notice with unset().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

did you test this behavior, or just suggesting?

Copy link

Choose a reason for hiding this comment

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

All I suggest I test before.

{
unset($this->observers[$observer]);
}
}

/**
* Gets the instance of the observer of class $observerClass
*
Expand Down