tags:

views:

34

answers:

1

I want to Constraint that the Derived Class must have a Default Constructor. I am currently thinking it in a perverted way

template <typename Derived>
class Base{
  public:
    Base(){

    }
    virtual ~Base(){
      new Derived;
    }
};

Another idea comes to mind is to keep a pure virtual create() method with no arguments.

But is there any other way ? Other than these two. I am thinking it in C++ way. But Is there any way to do this in PHP (I expect negetive answer LOL)

+1  A: 

Yes, there's a way in PHP LOL:

abstract class Base {
    public final function __construct() {
        $this->constructImpl();
    }
    abstract protected function constructImpl();
}

class Derived extends Base {
    protected function constructImpl() {
        /* implementation here */
    }
}

Basically, you just have to mark the constructor final.

Artefacto
WoW Man. You made me Laugh Loud. nice trick.better to say you are pulling PHP's leg.never thought it will be that easy to do in PHP. I should keep some faith on it. LOL