Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you declare an attribute private within an abstract class?

Tags:

oop

php

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?

like image 797
Hyder B. Avatar asked Feb 16 '15 11:02

Hyder B.


1 Answers

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.

like image 129
stwalkerster Avatar answered Oct 16 '22 13:10

stwalkerster