0

Bellow is a PHP script.

I tried to implement the Observer pattern (without MVC structure)... only basic.

The error which is encountered has been specified in a comment. First I tried to add User objects to the UsersLibrary repository. There was a error such as User::update() does not exists or something.

Why is that error encountered? What fix should be applied and how?

   interface IObserver {
       public function update(IObservable $sender);
   }
   interface IObservable   {
       public function addObserver(IObserver $obj);
       public function notify();
   }
   class UsersLibrary implements IObservable {
        private $container;
        private $contor;
    //private $z;
    public function __construct() {//IObserver $a)   {
        $this->container = array();
        $this->contor = 0;
        echo "<div>[constructing UsersLibrary...]</div>";
        $this->addObserver(new Logger());
        //$this->z = $a;
    }
    public function add($obj)  {
        echo "<div>[adding a new user...]</div>";
        $this->container[$this->contor] = $obj;
        $this->contor++;
        $this->notify();
    }
    public function get($index) {
        return $this->container[$index];
    }
    public function addObserver(IObserver $obj) {
        $this->container[] = $obj;
    }
    public function notify()    {
        echo "<div>[notification in progress...]</div>";
        foreach($this->container as $temp) {
            //echo $temp;
            #################################################################
            $temp->update(); //--------ERROR
        //Fatal Error: Call to a member function update() on a non-object.
            #################################################################
        }
        //$this->container[0]->update();
        //$this->z->update($this);
    }
}
class User   {
    private $id;
    private $name;
    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
    }
    public function getId() {
        return $this->id;
    }
    public function getName()   {
        return $this->name;
    }
}
class Logger implements IObserver   {
    public function __construct()   {
        echo "<div>[constructing Logger...]</div>";
    }
    public function update(IObservable $sender) {
        echo "<div>A new user has been added.</div>";
    }
}
$a = new UsersLibrary(); //new Logger());
//$a->add(new User(1, "DemoUser1"));
//$a->add(new User(2, "DemoUser2"));
$a->add("Demo");
echo $a->get(0);
//echo $a->get(0)->getName();
Zer
  • 13
  • 2

2 Answers2

1

Your User class is not implementing interface IObserver and therefore is not forced to have the method update().

You have to instantiate a new User() in order to add it to the UsersLibrary:

$library = new UsersLibrary();
$user = new User(1, "Demo");
$library->add($user);

Also, you are mixing Users and Loggers into your UsersLibrary container. Maybe think about separating the containers for them?

James Woodruff
  • 963
  • 6
  • 10
0

You are passing a string instead of an object in your $a->add() call. You should either pass in an object, or alter the code in UserLibrary::add() to wrap it's argument in an appropriate object (or do an object lookup of it sees a string, for instance find a user with that name).

$user = new User(1, "Demo");
$a = new UsersLibrary();
$a->add($user);
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55