Page 1 of 1

Abstracts in PHP4

Posted: Tue Jun 26, 2007 4:44 pm
by superdezign
I'm writing a class for public use, so I'd like to give a PHP4 version as well. However, the abstract has an abstract function... How do I show this through PHP4?

Posted: Tue Jun 26, 2007 5:36 pm
by feyd
PHP 4 doesn't support abstract, public, private, protected, static and a few other keywords.

Posted: Tue Jun 26, 2007 5:48 pm
by superdezign
I know, I was wondering how I could somehow let PHP 4 developers know that it was an abstract, but now I've decided just to not include a PHP 4 alternative. I'll just inform them on how to convert PHP 5 script into PHP 4 scripts.

Posted: Tue Jun 26, 2007 5:50 pm
by feyd
If you're going to do that, you may as well provide it for them.

Generally I create a dummy class for PHP 4 with either empty or trigger_error() filled methods.

Posted: Tue Jun 26, 2007 5:55 pm
by Christopher
Horrible, but:

Code: Select all

/* abstract */ class MyAbstract {

/* abstract */ function myFunction () {
    trigger_error("Abstract function MyAbstract::myFunction not declared.", E_USER_ERROR);
}

}

Posted: Tue Jun 26, 2007 6:04 pm
by stereofrog
Some people love to put c++ "pure virtual function called" spell in there, sounds kinda cool. ;)

Posted: Tue Jun 26, 2007 7:43 pm
by superdezign
arborint wrote:Horrible, but:

Code: Select all

/* abstract */ class MyAbstract {

/* abstract */ function myFunction () {
    trigger_error("Abstract function MyAbstract::myFunction not declared.", E_USER_ERROR);
}

}

I like that approach. Thanks for that.

Posted: Tue Jun 26, 2007 9:10 pm
by Begby
You can also use docblocks which is handy since IDE's can also catch it

Code: Select all

/**
 * My class is cool
 *
 * @abstract
*/
class MyAbstractClass
{

}

Posted: Tue Jun 26, 2007 9:11 pm
by John Cartwright
Bedby, can you explain what that has to do with imitating abstracts in php4?