0

Consider a class that prints an "outline" table for a particular product line. Say you have several, A, B, C, D. The mechanism of the table generation is the same, but the data is different. I could create a "base" class called OutlineTableGenerator, and one class per product line each that extend that base class. I can put data getters into individual product classes and let the base class operate on the data. Observe below:

class OutlineTableGenerator
{
    protected $vars;

    function __construct($vars)
    {
        $this->vars = $vars;        
    }

    public function getOutlineTable()
    {
        $data = $this->getOutlineTableData();  //my question is about this line
    }
}

and

class OutlineTableGeneratorForProductA extends OutlineTableGenerator
{
    function __construct($vars)
    {
        parent::__construct($vars); 
    }

    protected function getOutlineTableData()
    {
         return ['data' => $this->vars['stuff']];
    }
}

See line $data = $this->getOutlineTableData();. My problem with this line is that my IDE, nor any other IDE for that matter, will be able to figure out which class & function it will resolve to at run time. I quite frequently use a feature to jump from the line in the code where a function is called, to the actual function declaration. In this case, I using that feature on the line in question will not jump to function declaration. Because there are several of those, and nor IDE nor programming language itself knows which function I want. I think this is bad.

Is there a work-around? Is this a drawback of polymorphism in OO? How do YOU live with it?

2
  • 3
    This is not a failure. It is an ambiguity that your IDE should be help to help you understand by giving you a list of methods that might be called at run time. Oct 15, 2014 at 22:02
  • Polymorphism is an effect of the runtime type system, I don't know PHP so I can't say - but does your code here work ? If so, I don't think any polymorphism failure is occurring, it's functioning as you expect, but rather your IDE which doesn't have a full PHP runtime in it doesn't know the behaviour that's actually going to occur at runtime. IDE's don't typically know everything about your code, so it's not uncommon for them to not recognize exactly what's going to happen. For the IDE to know what will happen it would require the full PHP type system implemented in it. Oct 15, 2014 at 22:11

1 Answer 1

2

A failure in polymorphism? I guess that depends on how you use the classes.

Do you ever instantiate an OutlineTableGenerator object? Because I would expect any such object to cause a fatal error if its getOutlineTable method is ever called.

It looks like you really have an abstract class there; one which is never instantiated directly, but can be extended and also used for type hinting within parameter lists.

abstract class OutlineTableGenerator {
   ...
    public function getOutlineTable() {
        $data = $this->getOutlineTableData(); //my question is about this line
   }

    abstract function getOutlineTableData();
}

My IDE will usually then offer me a list of implementations whenever I follow the reference for such an abstract function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.