Question

I am new at design patterns. And I have a log-in system with classes to connect to my database and stuff like that.

But now I want to include a observer pattern in my PHP code. But I don't know how do to that. For example a user I notified whenever there is a new user. I know how the observer pattern works, what it does for example. But I don't know how to include it to PHP code. So how do you do a login system that includes observer pattern?

For example here is my connection class to my database:

private $pdo;

function __construct() {
    $this->pdo = new PDO('mysql:host=localhost;dbname=users', '', '');
}

And heres is the code i using in the login file:

if(isset($_POST['username']) && isset($_POST['password'])) {
    include_once("classes/database.php");
    $db = new DB();
    $result = $db->query("SELECT username, pass FROM users WHERE username='".$_POST['username']."' AND pass='".$_POST['password']."'");

    if(isset($result[0]['username']) && isset($result[0]['password'])) {
        $_SESSION['username'] = $_POST['username'];
        header("Location: start.php?username=".$_SESSION['username']);
    }
Was it helpful?

Solution

This is an example that uses the Observer pattern to register observers to your login system. In the example, i register an observer that sends a email to the admin when a new user is created:

First, we create the interfaces that define the behavior of the observable/observers

interface IObserver
{
     //an observer has to implement a function, that will be called when the observable changes
     //the function receive data (in this case the user name) and also the observable object (since the observer could be observing more than on system)
     function onUserAdded( $observable, $data );
}

interface IObservable
{
    //we can attach observers to the observable
    function attach( $observer );
}

Then you let your main login system implement IObservable, you implement the attach function, and keep an array of observers. internally, you implement a notify function that when it is called, iterate the array and notify every observer.

What you do, is after all the normal behavior of creating an user, you call your notify function.

class LoginSystem implements IObservable
{
    private $observers = array();

    private function notify( $userName )
    {
        foreach( $this->observers as $o )
            $o->onUserAdded( $this, $userName );
    }

    public function attach( $observer )
    {
        $this->observers []= $observer;
    }

    public function createUser( $userName )
    {

        //all your logic to add it to the databse or whatever, and then:

        $this->notify($userName);

    }

}

Then, you create a class than implements Iobserver. in the onUserAdded function, you do whatever you want to happen when the observed event occurs. In this example i just send a mail to a hardcoded email. It could be as complex as you want.

class NewUserMailer implements IObserver
{
    public function  onUserAdded( $observable, $data ) 
    {
        mail("admin@yoursite.com", "new user", $data);
    }
}

Then, the behavior is just, after creating the loginsystem, you also create an observer, and attach is to the login system

$ls = new LoginSystem();
$ls->attach( new NewUserMailer() );

With this, everytime a new user is created, a mail will be sent to "admin@yoursite.com"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top