147

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';   
}
Tamás Pap
  • 17,777
  • 15
  • 70
  • 102

12 Answers12

166

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.

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
  • 79
    There is no obvious reason the word 'abstract' could not be used on *static* properties - but with a slightly different meaning. For example it could indicate that a subclass has to provide a value for the property. – frodeborli Aug 05 '14 at 09:32
  • 5
    In TypeScript there are [abstract properties and accessors](https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#abstract-properties-and-accessors). It is sad that in php it is impossible. – Илья Зеленько Dec 04 '18 at 12:04
63

No, there is no way to enforce that with the compiler, you'd have to use run-time checks (say, in the constructor) for the $tablename variable, e.g.:

class Foo_Abstract {
  public final function __construct(/*whatever*/) {
    if(!isset($this->tablename))
      throw new LogicException(get_class($this) . ' must have a $tablename');
  }
}

To enforce this for all derived classes of Foo_Abstract you would have to make Foo_Abstract's constructor final, preventing overriding.

You could declare an abstract getter instead:

abstract class Foo_Abstract {
  abstract public function get_tablename();
}

class Foo extends Foo_Abstract {
  protected $tablename = 'tablename';
  public function get_tablename() {
    return $this->tablename;
  }
}
connec
  • 7,231
  • 3
  • 23
  • 26
  • Nice feature, i like how you implement abstract properties. – Mathieu Dumoulin Oct 03 '11 at 12:47
  • 4
    This would require you to make the constructor final in the abstract base class. – hakre Oct 03 '11 at 13:06
  • @hakre: I'm not sure I understand you? – connec Oct 03 '11 at 15:40
  • 3
    Some explanation: If you do the check inside the constructor and if it should be mandatory, you need to ensure that it gets executed on every instance instantiation. Therefore you need to prevent that it gets removed, e.g. by extending the class and replacing the constructor. The [final keyword](http://php.net/manual/en/language.oop5.final.php) would you allow to do so. – hakre Oct 03 '11 at 15:48
  • 2
    I like the "abstract getter" solution. When you declare a function in a class abstract, you must declare the class itself abstract. This means the class is unusable unless extended and fully implemented. When extending that class, you must provide an implementation for the "getter" function. This means that you must also create a related property inside the extending class, because the function must have something to return. Following this pattern you get the same result as if you would by declaring an abstract property, it's also a clean and clear approach. That's how it's actually done. – Salivan Aug 02 '15 at 12:18
  • 1
    Using an abstract getter also allows you to implement it by **generating** a value, as opposed to returning a constant value, when it makes sense to do so. An abstract property would not allow you to do so, especially a static property. – Tobia Jul 11 '16 at 15:20
  • Yes. Abstract getter is the way to go. It does not enforce implementation details, e.g. you could name your property differently and return it in the getter, or as sayed generate, validate, or do whatever you like with it. – Tobias Gaertner Sep 01 '23 at 05:44
44

Depending on the context of the property, if I want to force declaration of an abstract class property in an extended class, I like to use a constant with the static keyword for the property in the abstract object constructor or setter/getter methods. You can optionally use final to prevent the method from being overridden in extended classes.

Example: https://3v4l.org/WH5Xl

abstract class AbstractFoo
{
    public $bar;

    final public function __construct()
    {
        $this->bar = static::BAR;
    }
}

class Foo extends AbstractFoo
{
    //const BAR = 'foobar'; //uncomment to prevent exception
}
$foo = new Foo(); 
//Fatal Error: Undefined class constant 'BAR'

However, the extended class overrides the parent class properties and methods if redefined.
For example; if a property is declared as protected in the parent and redefined as public in the extended class, the resulting property is public. Otherwise, if the property is declared private in the parent it will remain private and not available to the extended class.

http://www.php.net//manual/en/language.oop5.static.php

Will B.
  • 17,883
  • 4
  • 67
  • 69
26

As stated above, there is no such exact definition. I, however, use this simple workaround to force the child class to define the "abstract" property:

abstract class Father 
{
  public $name;
  abstract protected function setName(); // now every child class must declare this 
                                      // function and thus declare the property

  public function __construct() 
  {
    $this->setName();
  }
}

class Son extends Father
{
  protected function setName()
  {
    $this->name = "son";
  }

  function __construct(){
    parent::__construct();
  }
}
ulkas
  • 5,748
  • 5
  • 33
  • 47
  • Elegant, but doesn't solve the issue for `static` properties. – Robbert Dec 21 '14 at 20:01
  • 1
    I don't think you can have private for abstract methods. – Zorji Jan 13 '15 at 01:11
  • @Phate01 as i understand it, in the comment itself it states `the only "safe" methods to have in a constructor are private and/or final ones`, isnt my workaround such a case? im using privates in it – ulkas Apr 16 '15 at 08:46
  • You don't need the constructor in the child class (Son). The parent will be called automatically if you leave it out. – voidstate Aug 04 '15 at 09:06
  • Great answer ! Note that maybe `abstract protected function setName();` is better. – Rey0bs Mar 01 '16 at 10:50
  • @sboye yes you are right, abstract private methods are not allowed in php (i must have written this by heart). i have edited my answer – ulkas Mar 02 '16 at 08:28
  • 7
    This looks nice, but it doesn't force a child class to actually set `$name`. You could implement the `setName()` function without it actually setting `$name`. – JohnWE May 09 '16 at 20:20
  • 4
    I think that using `getName` instead of `$name` works better. `abstract class Father { abstract protected function getName(); public function foo(){ echo $this->getName();} }` – Hamid Sep 27 '16 at 07:00
12

The need for abstract properties can indicate design problems. While many of answers implement kind of Template method pattern and it works, it always looks kind of strange.

Let's take a look at the original example:

abstract class Foo_Abstract {
    abstract public $tablename;
}

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

To mark something abstract is to indicate it a must-have thing. Well, a must-have value (in this case) is a required dependency, so it should be passed to the constructor during instantiation:

class Table
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function name(): string
    {
        return $this->name;
    }
}

Then if you actually want a more concrete named class you can inherit like so:

final class UsersTable extends Table
{
    public function __construct()
    {
        parent::__construct('users');
    }
}

This can be useful if you use DI container and have to pass different tables for different objects.

sevavietl
  • 3,762
  • 1
  • 14
  • 21
8

I've asked myself the same question today, and I'd like to add my two cents.

The reason we would like abstract properties is to make sure that subclasses define them and throw exceptions when they don't. In my specific case, I needed something that could work with statically.

Ideally I would like something like this:

abstract class A {
    abstract protected static $prop;
}

class B extends A {
    protected static $prop = 'B prop'; // $prop defined, B loads successfully
}

class C extends A {
    // throws an exception when loading C for the first time because $prop
    // is not defined.
}

I ended up with this implementation

abstract class A
{
    // no $prop definition in A!

    public static final function getProp()
    {
        return static::$prop;
    }
}

class B extends A
{
    protected static $prop = 'B prop';
}

class C extends A
{
}

As you can see, in A I don't define $prop, but I use it in a static getter. Therefore, the following code works

B::getProp();
// => 'B prop'

$b = new B();
$b->getProp();
// => 'B prop'

In C, on the other hand, I don't define $prop, so I get exceptions:

C::getProp();
// => Exception!

$c = new C();
$c->getProp();
// => Exception!

I must call the getProp() method to get the exception and I can't get it on class loading, but it is quite close to the desired behavior, at least in my case.

I define getProp() as final to avoid that some smart guy (aka myself in 6 months) is tempted to do

class D extends A {
    public static function getProp() {
        // really smart
    }
}

D::getProp();
// => no exception...
Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
6

As you could have found out by just testing your code:

Fatal error: Properties cannot be declared abstract in ... on line 3

No, there is not. Properties cannot be declared abstract in PHP.

However you can implement a getter/setter function abstract, this might be what you're looking for.

Properties aren't implemented (especially public properties), they just exist (or not):

$foo = new Foo;
$foo->publicProperty = 'Bar';
hakre
  • 193,403
  • 52
  • 435
  • 836
4

PHP 7 makes it quite a bit easier for making abstract "properties". Just as above, you will make them by creating abstract functions, but with PHP 7 you can define the return type for that function, which makes things a lot easier when you're building a base class that anyone can extend.

<?php

abstract class FooBase {

  abstract public function FooProp(): string;
  abstract public function BarProp(): BarClass;

  public function foo() {
    return $this->FooProp();
  }

  public function bar() {
    return $this->BarProp()->name();
  }

}

class BarClass {

  public function name() {
    return 'Bar!';
  }

}

class FooClass extends FooBase {

  public function FooProp(): string {
    return 'Foo!';
  }

  public function BarProp(): BarClass {
    // This would not work:
    // return 'not working';
    // But this will!
    return new BarClass();
  }

}

$test = new FooClass();
echo $test->foo() . PHP_EOL;
echo $test->bar() . PHP_EOL;
Dropa
  • 41
  • 2
1

if tablename value will never change during the object's lifetime, following will be a simple yet safe implementation.

abstract class Foo_Abstract {
    abstract protected function getTablename();

    public function showTableName()
    {
        echo 'my table name is '.$this->getTablename();
    }
}

class Foo extends Foo_Abstract {
    //Foo must 'implement' getTablename()
    protected function getTablename()
    {
        return 'users';
    }
}

the key here is that the string value 'users' is specified and returned directly in getTablename() in child class implementation. The function mimics a "readonly" property.

This is fairly similar to a solution posted earlier on which uses an additional variable. I also like Marco's solution though it can be a bit more complicated.

ck.tan
  • 99
  • 3
1

Just define the property in the base class without assigning it a (default) value. Getting the property value without redefining it with a default value or assigning it a value will throw an Error.

<?php

class Base {
    protected string $name;

    public function i_am() : string {
        return $this->name;
    }
}

class Wrong extends Base {
    ...
}

class Good extends Base {

    protected string $name = 'Somebody';

}

$test = new Good();
echo $test->i_am(), '<br>';  // Will show "Nobody"

$test = new Wrong();
echo $test->i_am(), '<br>';  // Will throw an Error:
                             // Error: Typed property Base::$name must not be accessed before initialization in ....


?>
0

You can define a static property in an abstract class.

<?php
abstract class Foo {
    private static $bar = "1234";
    
    public static function func() {
        echo self::$bar;
    }
}


Foo::func(); // It will be printed 1234
Walter
  • 108
  • 5
-3

Too late to answer the question, but you may use the difference between self and static as follows

<?php
class A { // Base Class
    protected static $name = 'ClassA';
    public static function getSelfName() {
        return self::$name;
    }
    public static function getStaticName() {
        return static::$name;
    }
}

class B extends A {
    protected static $name = 'ClassB';
}

echo A::getSelfName(); // ClassA
echo A::getStaticName(); // ClassA

echo B::getSelfName(); // ClassA
echo B::getStaticName(); // ClassB
M a m a D
  • 1,938
  • 2
  • 30
  • 61