PHP abstract properties

前端 未结 9 1442
终归单人心
终归单人心 2020-12-07 16:16

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

abstract class Foo_Abstract {
    abstract public $tablename;
}

class Foo extends Foo_Abstract          


        
9条回答
  •  猫巷女王i
    2020-12-07 16:55

    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';
    

提交回复
热议问题