Page 1 of 1

Abstract Classes and Constructors

Posted: Wed Jul 19, 2006 11:34 am
by Son Volt
If I have an abstract class, I assume since it can't be instantiated there is no constructor for the class. I want to make sure each class that extends from this abstract class defines a constructor. Does that mean I need to define an abstract method called __construct() in my abstract class to force this?

Code: Select all

abstract class Person{
    
    protected $name;

    function getName(){
        return $this->name;
    }
   
    abstract function __construct(); //enforce constructor here???
}


class Employee extends Person{
    
    function __construct(){ 
        $this->name = "john";
    }
}

Posted: Wed Jul 19, 2006 11:42 am
by Christopher
Yes, defining __contruct() as abstract will require that a constructor exists and give a fatal error if it is missing.