Can you declare an attribute private within an abs

2019-06-20 04:19发布

Let's say you have an abstract class:

abstract class PersonAbstract
{
    private $name = "Stack Overflow";
}

Is it legal to declare an attribute as private within an abstract class? Or the fact that this class should be extended, the minimum visibility is protected?

标签: php oop
1条回答
看我几分像从前
2楼-- · 2019-06-20 04:56

Yes, you can have a private field within an abstract class. This field will only be accessible to functions within that abstract class though. Any classes which inherit from your abstract class will not be able to access the field.

You can declare both fields and functions as public, protected or private within an abstract class. If a field or function is public, it is accessible to anyone. If it is protected, it is accessible only to that class, and any classes which inherit from that class. If it is private, it is only accessible to that class.

Abstract functions must be implemented by an inheriting class, so it makes no sense (and probably won't work) to have a private abstract function.

查看更多
登录 后发表回答