PHP抽象属性 [英] PHP abstract properties

查看:21
本文介绍了PHP抽象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 PHP 中定义抽象类属性?

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

推荐答案

没有定义属性这样的东西.

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"仅表示已声明但未定义的东西,因此在使用它之前,您需要定义它,否则它就变得无用了.

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

这篇关于PHP抽象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆