Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:39:18+00:00 2026-06-14T05:39:18+00:00

I’m playing with some design patterns, and wanted to create an example using the

  • 0

I’m playing with some design patterns, and wanted to create an example using the SPL’s observer pattern. Because it doesn’t make sense to have observers and subjects be completely generic, I wanted to extend the interfaces to make them more specific to the application at hand. The problem is that when I run the code below, I get errors like “DataAccess::update() must be compatible with that of SplObserver::update()”.

I know that I can make this code execute without errors by switching the method signatures to match those of the interfaces. My question is this: Why doesn’t it allow children of the classes defined in the signatures? Below, ModelObserver is a SplObserver, and Model is a SplSubject. I would have expected this to work. Am I missing something?

FYI, I know I could use the explicit method signatures as defined in the interface and use the instanceof keyword in my code logic to achieve the same thing. I was just hoping to find a more elegant solution. Thanks!

<?php
interface ModelObserver extends SplObserver {
}

class DataAccess implements ModelObserver {

    /*
     * (non-PHPdoc) @see SplObserver::update()
     */
    public function update(Model $subject) {
        // TODO Auto-generated method stub
    }
}

// Just a generic model for the example
class Model implements SplSubject {
    private $_properties = array ();
    private $_observers = array ();

    /*
     * generically handle properties you wouldn't want to do it quite like this
     * for a real world scenario
     */
    public function __get($name) {
        return $this->_properties [$name];
    }
    public function __set($name, $value) {
        $this->_properties [$name] = $value;
    }
    public function __call($method, $args) {
        if (strpos ( $method, 'get' ) === 0) {
            $name = lcfirst ( str_replace ( 'get', '', $method ) );
            return $this->_properties [$name];
        }

        if (strpos ( $method, 'set' ) === 0) {
            $name = lcfirst ( str_replace ( 'set', '', $method ) );
            $this->_properties [$name] = $args [0];
            return $this;
        }
    }
    public function __toString() {
        return print_r ( $this, true );
    }

    /*
     * (non-PHPdoc) @see SplSubject::attach()
     */
    public function attach(ModelObserver $observer) {
        $this->_observers [] = $observer;
        return $this;
    }

    /*
     * (non-PHPdoc) @see SplSubject::detach()
     */
    public function detach(ModelObserver $observer) {
        if (in_array ( $observer, $this->_observers )) {
            $f = function ($value) {
                if ($value != $observer) {
                    return $value;
                }
            };
            $observers = array_map ( $f, $this->_observers );
        }
        return $this;
    }

    /*
     * (non-PHPdoc) @see SplSubject::notify()
     */
    public function notify() {
        foreach ($this->_observers as $observer) {
            $observer->update($this);
        }
    }
}

$da = new DataAccess();

$model = new Model ();
$model->setName ( 'Joshua Kaiser' )->setAge ( 32 )->setOccupation ( 'Software Engineer' )
    ->attach($da);

echo $model;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:39:19+00:00Added an answer on June 14, 2026 at 5:39 am

    Limiting DataAccess::update() to accept your child Model breaks the contract of this interface.

    True, all Model objects are of class SplSubject , but not all SplSubject are of class Model. An interface is a contract guaranteeing that an implementing class it supports everything the interface supports.

    Your code, if it worked would be limiting the DataAccess::update() method to only the Model sub class and not the wider parent class SplSubjects . You cannot narrow the scope of the parameter passed to method defined by an interface.

    Let’s say you added a property public $foo to the Model class. If it were allowed, you could in your DataAccess::update() method you uses that property $foo. Someone could come along and extend SplSubjects to a child OddModel which didn’t have a $foo property. They could no longer pass the OddModel into your DataAccess::update() function–if they could it would break as no $foo property exists for the OddModel.

    This is the whole idea behind interfaces, by implementing they you agree 100% to support what is defined by the interface. In this case your interface says:

    if you implement me, you must accept every SplSubject or class that extends SplSubject

    You’re implementation of the interface attempts to break the contract.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.