This is a discussion on abstract classes and inheritance in php5 within the alt.comp.lang.php forums, part of the PHP Programming Forums category; abstract class Foo { final function __construct() { } abstract function init(); } class Parser extends Foo { function init() { } } class CssParser extends Parser { } /* in ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
abstract class Foo {
final function __construct() { } abstract function init(); } class Parser extends Foo { function init() { } } class CssParser extends Parser { } /* in a code like the above, how can you force all the inherited classes to implement a method like init() ? */ |
|
|||
|
Ollie wrote:
> in a code like the above, how can you force all the inherited classes > to implement a method like init() ? > Not possible, because only the class that extends the abstract class is forced to implement the method. Other classes just inherit the method. JW |
|
|||
|
Janwillem Borleffs wrote:
> Not possible, because only the class that extends the abstract class > is forced to implement the method. > > Other classes just inherit the method. > However, there is a way using the Reflection API: abstract class Foo { final function __construct() { $rm = new ReflectionMethod($this, 'init'); if (get_class($this) != $rm->getDeclaringClass()->getName()) { throw new Exception("init() method must be implemented"); } } abstract function init(); } But be aware that using the Reflection API in production applications can have a negative impact on performance. JW |