Can you declare an attribute private within an abs

2019-06-20 04:03发布

问题:

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?

回答1:

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.



标签: php oop