abstract classes and inheritance in php5

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2005
Ollie
 
Posts: n/a
Default abstract classes and inheritance in php5

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() ?
*/


Reply With Quote
  #2 (permalink)  
Old 05-28-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: abstract classes and inheritance in php5

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



Reply With Quote
  #3 (permalink)  
Old 05-28-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: abstract classes and inheritance in php5

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



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:12 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0