Abstract classes

I've got an abstract class:

abstract class dbTable {
abstract static function fn1();
static function update()
{
// blah
$var = self::fn1();
// blah
}
}

And then sombody that uses it:

static class example extends dbTable {
static function fn1( ) { // blah
}

static fn2()
{
self::update();
}

}

The problem I'm running into is that the self::fn1() reference in the
abstract class is failing with a Cannot call abstract method fn1()
when I try to invoke it indirectly through dbTest.

Note: I do want them to be static classes as the lifetime of the usage
is not worth creating an object, calling 1 function, and then
destroying it.
ctalkobt [ Mo, 02 Juli 2007 22:52 ] [ ID #1757617 ]

Re: Abstract classes

On Jul 2, 4:52 pm, Craig Taylor <ctalk... [at] gmail.com> wrote:
> I've got an abstract class:
>
> abstract class dbTable {
> abstract static function fn1();
> static function update()
> {
> // blah
> $var = self::fn1();
> // blah
> }
>
> }
>
> And then sombody that uses it:
>
> static class example extends dbTable {
> static function fn1( ) { // blah
> }
>
> static fn2()
> {
> self::update();
> }
>
> }
>
> The problem I'm running into is that the self::fn1() reference in the
> abstract class is failing with a Cannot call abstract method fn1()
> when I try to invoke it indirectly through dbTest.
>
> Note: I do want them to be static classes as the lifetime of the usage
> is not worth creating an object, calling 1 function, and then
> destroying it.

static methods are not inherited. In fact, as of PHP5, you'll get a
warning if you try to define a method as "abstract static." The
symbol "self" is bound at compile time -- basically it gets replaced
with whatever the class name is -- so, inside dbTable, self is turned
into dbTable. When you call fn1 from inside update, you're basically
calling dbTable::fn1 and, since that method is abstract, it can't be
called.
zeldorblat [ Mo, 02 Juli 2007 23:45 ] [ ID #1757618 ]
PHP » alt.php » Abstract classes

Vorheriges Thema: includes and keywords
Nächstes Thema: Sharing Stuff For Web Developers