SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Interface Location

    Probably very obvious, but it's late and I'm having a little session of "alcohol fuelled development" but could someone explain why I'm receiving the error:-

    Fatal error: Can't inherit abstract function EnglishBreakfast::hasHashBrowns() (previously declared abstract in NorthernBreakfast) in C:\webroot\www\f.php on line 17

    PHP Code:
    <?php
    interface EnglishBreakfast
    {
        public function 
    getBeans();

        public function 
    getBacon();

        public function 
    getSausage();

        public function 
    getEggs();

        public function 
    getToast();

        public function 
    hasHashBrowns();
    }

    abstract class 
    NorthernBreakfast implements EnglishBreakfast
    {
        abstract public function 
    hasHashBrowns();

        public function 
    getBeans()
        {
            return 
    'Heinz';
        }

        public function 
    getBacon()
        {
            return 
    'Danish Back';
        }

        public function 
    getSausage()
        {
            return 
    'Cumberland';
        }

        public function 
    getEggs()
        {
            return 
    'Scrambled';
        }

        public function 
    getToast()
        {
            return 
    'Dark, both sides, dry';
        }

        public function 
    hasBlackPudding()
        {
            return 
    true;
        }
    }

    class 
    SilversBreakfast extends NorthernBreakfast
    {
        public function 
    hasHashBrowns()
        {
            return 
    'of course!';
        }
    }

    class 
    Meals
    {
        protected 
    $aMealsToday = array();

        public function 
    setBreakfast(EnglishBreakfast $oBreakfast)
        {
            
    array_push($this->aMealsToday$oBreakfast);
        }
    }

    $oMeals = new Meals();

    $oMeals->setBreakfast(new SilversBreakfast());
    ?>
    Surely it must be possible for an object to implement an interface whilst leaving a child object to fulfil an abstract method, even moving the declaration of the interface implementation to the child object fails with the same error.

    Should I just stop coding and go to bed?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Maidenhead, UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of interest.... what exactly were you drinking? I'm going to get some ice and pour a bit of Bells over it. Might help read that yummy code of yours
    -------------------------------------------------------
    UK postcode->address lookup : www.CraftyClicks.co.uk
    a piece of my brain : adam.stylo.name (at your own risk)
    -------------------------------------------------------

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Maybe it will, currently Vodka from the freezer. Mrs.SilverBulletUK is out with the girls tonight so I get to sing to bad 80's music, code bad PHP and no one will know!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn you and your breakfast with bacon posts...make me hungry lol

    If you think about it, there's no reason you would want/need to declare the method signature again in NorthernBreakfast at all unless you actually implement it. You can't change the signature, so either you just inherit the abstract method and let a subclass provide the implementation, or you provide the implementation there.

    So long as the class is abstract, you don't need to provide implementations for any inherited abstract methods(all the stuff from your interface).

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I do like bacon. *Homer Simpson style drool*

    I found a presentation by Udi Dahan where he indicates, for maximum flexibility, you code to interfaces throughout the whole application.

    Every object has an interface and, where composition is used, accepts any object which implements a specific interface.

    I doubt this is ground breaking, it makes far too much sense not to be, but I've never approached building application in this method so thought I'd give it a go.

    I was trying to figure out a way to have a base object which MUST be extended, this object would implement an interface which would be required in another object.

    You hit the nail on the head crmalibu and pointed me in the right direction, cheers! I think it was case of 'not being able to see the forest for the trees'.

    Here's the non-bacon related way I did it.
    PHP Code:
    <?php
    interface ISwitchable
    {
        public function 
    switchOn();

        public function 
    switchOff();
    }

    class 
    RemoteControl
    {
        public function 
    addSwitchableDevice(ISwitchable $oDevice)
        {
        }
    }

    abstract class 
    BaseTV
    {
    }

    class 
    ImprovedTVWithSwitch extends BaseTV implements ISwitchable
    {
        public function 
    switchOn(){}

        public function 
    switchOff(){}
    }

    $oRemote = new RemoteControl();

    $oRemote->addSwitchableDevice(new ImprovedTVWithSwitch());
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •