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 6941141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:50:50+00:00 2026-05-27T12:50:50+00:00

As only Dogs can play fetch, is this example a good or a bad

  • 0

As only Dogs can play “fetch”, is this example a good or a bad idea? I suspect it’s a really bad idea due to the usage of instanceof, but I’m not entirely sure why.

class Animal {
    var $name;
    function __construct($name) {
        $this->name = $name;
    }
}

class Dog extends Animal {
    function speak() {
        return "Woof, woof!";
    }

    function playFetch() {
        return 'getting the stick';
    }
}

class Cat extends Animal {
    function speak() {
        return "Meow...";
    }
}

$animals = array(new Dog('Skip'), new Cat('Snowball'));

foreach($animals as $animal) {
    print $animal->name . " says: " . $animal->speak() . '<br>';
    if ($animal instanceof Dog) echo $animal->playFetch();
}

Another example. As I am constantly creating data objects that have an ID, I figured I might as well extend them all from a base class to avoid code duplication. Again, this was bad right? As a Chair doesn’t have a name and a Dog doesn’t have wheels. But they are both Data Objects so it’s very confusing.

class Data_Object {
    protected $_id;

    function setId($id) {
        $this->_id = $id;
    }

    function getId() {
        return $this->_id;
    }
}

class Dog extends Data_Object {
    protected $_name;
    function setName($name) {
        $this->_name = 
    }

    function getName() {
        return $this->_name;
    }
}

class Chair extends Data_Object {
    protected $_numberOfWheels;
    function setNumberOfWheels($number) {
        $this->_numberOfWheels = $number;
    }

    function getNumberOfWheels() {
        return $this->_numberOfWheels;
    }
}

Essentially what I think I’m asking is: “should all subclasses have the same interface or can they have different ones?”

  • 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-05-27T12:50:51+00:00Added an answer on May 27, 2026 at 12:50 pm

    In this context it’s useful to talk about interfaces.

    interface Talkative {
        public function speak();
    }
    
    class Dog extends Animal implements Talkative {
        public function speak() {
            return "Woof, woof!";
        }
    }
    

    Any animal or human (or alien) that implements the Talkative interface can be used in a context where talkative beings are needed:

    protected function makeItSpeak(Talkative $being) {
        echo $being->speak();
    }
    

    This is a properly used polymorphic method. You don’t care what you’re dealing with as long as it can speak().

    If Dogs can also play fetch, that’s great for them. If you want to generalize that, think about it in terms of an interface as well. Maybe one day you’ll get a highly trained cat which can play fetch as well.

    class Cog extends Cat implements Playfulness {
        public function playFetch() { ... }
    }
    

    The important point here being that when you call playFetch() on something, it’s because you want to play fetch with that animal. You don’t call playFetch because, well… you can, but because you want to play fetch in this very moment. If you don’t want to play fetch, then you don’t call it. If you need to play fetch in a certain situation, then you need something that can play fetch. You ensure this through interface declarations.

    You can achieve the same thing using class inheritance, it’s just less flexible. In some situations where rigid hierarchies exist though it’s perfectly useful:

    abstract class Animal { }
    
    abstract class Pet extends Animal { }
    
    class Dog extends Pet {
        public function playFetch() { ... }
    }
    
    class GermanShepherd extends Dog {
        public function beAwesome() { ... }
    }
    

    Then, in some specific context, you may not require any object that can do something (interface), but you are specifically looking for a GermanShepherd, because only it can be awesome:

    protected function awesomeness(GermanShepherd $dog) {
        $dog->beAwesome();
    }
    

    Maybe down the road you’ll make a new breed of GermanShepherds that are also awesome, but extend the GermanShepherd class. They’ll still work with the awesomeness function, just like with interfaces.

    What you certainly should not do is to loop through a bunch of random things, check what they are and make them do their own thing. That’s just not very sensible in any context.

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

Sidebar

Related Questions

How to get all dogs only? In C#, you can use animals.OfType<Dog>() , is
Only one instance of a Script Manager can be added to the page. Such
Using only VBScript (launched from Windows Scripting Host) can I pull the DLL metadata
Only certain users can no longer receive notifications from my app. I was able
Only a full week on emacs in windows now, I can feel the CTRLkey
I have been playing around with this problem since yesterday. I can't seem to
I wrote this code and it works fine when there's only 2 menus, but
Only the first nested linear layout is showing up, any idea why? The first
Only 10 hours ago, if I call $facebook->getUser() , I can get the correct
(Only managed to find one other question on this - Object persistence strategy for

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.