JObserver

From Joomla! Documentation
(Difference between revisions)
Jump to: navigation, search
(New page: '''JObserver''' is an abstract class which provides basic functionality to use the Subject/Observer pattern within your Joomla Application. ===Availability=== {{JVer|1.5|From Joomla 1.5}}...)
 
m (bad link repair)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''JObserver''' is an abstract class which provides basic functionality to use the Subject/Observer pattern within your Joomla Application.
+
This class is available in the following Joomla versions:-
 
+
<splist showpath=notparent />
===Availability===
+
<noinclude>[[Category:Platform JClasses]][[Category:JObserver]]</noinclude>
{{JVer|1.5|From Joomla 1.5}} {{JVer|1.6|Joomla 1.6}}
+
 
+
===Defined in===
+
/libraries/joomla/base/observer.php
+
 
+
===Extends===
+
* [[JObject]]
+
 
+
 
+
===Methods===
+
{| class="wikitable"
+
|-
+
!Method name
+
!Description
+
|-
+
|[[JObserver/__construct|__construct]]
+
|Constructor. Creates an internal reference to the subject. Also, attaches the observer to the subject.
+
|-
+
|[[JObserver/update|update]]
+
|Update method. This is called, when the <code>$subject->notify()</code> method is triggered.
+
|}
+
===Importing===
+
<source lang="php">jimport( 'joomla.base.observer' );</source>
+
 
+
==Examples ==
+
Let's say, we want to integrate Error-Handling in our application, but we don't know yet, if we want the error to be display, logged in a file, stored to the database, or all of the above. Our goal is, to keep the object that raises the error independent of the objects that store the error message.
+
 
+
<source lang="php">
+
class MyError extends JObservable {
+
  public $msg = NULL;
+
 
+
  function raiseError($msg){
+
      $this->msg = $msg;
+
      //Notify all attached ErrorHandlers of the state change.
+
      $this->notify();
+
  }
+
}
+
 
+
//We now implement the Observers, thus the error handlers
+
class ErrorHandlerDisplay extends JObserver {
+
  function update(){
+
      echo $this->subject->msg;
+
  }
+
}
+
class ErrorHandlerFileStorage extends JObserver {
+
  function update(){
+
      error_log($this->subject->msg;);
+
  }
+
}
+
class ErrorHandlerDB extends JObserver {
+
  function update(){
+
      $db = JFactory::getDBO();
+
      $sql = "INSERT INTO #__myerrors (message) VALUES (".$db->quote($this->subject->msg).")";
+
      $db->setQuery($sql);
+
      $db->query();
+
  }
+
}
+
 
+
//Now we can use newly implemented MyError class to raise Errors.
+
$error = new MyError();
+
 
+
/* The constructor of the observers automatically attaches the observer to the subject
+
* In our example that means that the constructor of the error handler automatically
+
* attaches the handler to the MyError Object.
+
*/
+
new ErrorHandlerDisplay($error);
+
new ErrorHandlerFileStorate($error);
+
new ErrorHandlerDB($error);
+
 
+
$error->raiseError('Help!!!');
+
 
+
/*
+
* Would cause 'Help!!!' to be display, logged in a file, and stored in the database.
+
* You can simply add and remove the error handlers as you like
+
*/
+
</source>
+
 
+
What happened here? We separated the functionality of raising an error of the functionality of handling an error. In the future you can add additional ErrorHandlers, or remove some of the existing handlers, without the need to change any classes at all. Furthermore you can simply change an Errorhandler, without the need to change the MyError class. This creatly increases the reusability of your code.
+
===See also===
+
* [http://api.joomla.org/Joomla-Framework/Base/JObserver.html JObserver on api.joomla.org]
+
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JObserver]]</noinclude>
+

Latest revision as of 13:58, 29 August 2012

This class is available in the following Joomla versions:-

Page "JObserver" has no subpages to list.
Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox