PHP abstract properties

Tamás Pap picture Tamás Pap · Oct 3, 2011 · Viewed 79.8k times · Source

Is there any way to define abstract class properties in PHP?

abstract class Foo_Abstract {
    abstract public $tablename;
}

class Foo extends Foo_Abstract {
    //Foo must 'implement' $property
    public $tablename = 'users';   
}

Answer

Mathieu Dumoulin picture Mathieu Dumoulin · Oct 3, 2011

There is no such thing as defining a property.

You can only declare properties because they are containers of data reserved in memory on initialization.

A function on the other hand can be declared (types, name, parameters) without being defined (function body missing) and thus, can be made abstract.

"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.