Answer the question
In order to leave comments, you need to log in
Is it possible to declare an abstract property in an abstract class (in PHP)?
Good day. Recently, during an interview, I came across a question that still haunts me. The question was: "Is it possible to declare an abstract property in an abstract class, and if not, why?". Subconsciously I understand that it is impossible, because. this is wrong (probably). But I'm not sure about this and I can't formulate why not. Does anyone have any thoughts on this? Is it possible, and if not, why not? =)
Answer the question
In order to leave comments, you need to log in
The same property, it is, so to speak, already implemented. Those. cannot be abstract by definition.
PS This does not make sense at all, since all the salt in abstraction is a description, without implementation.
Imagine that you are developing an API for sending a read and you wanted to implement a few more options in addition to the build-in implementation of sending mail (the mail function), for example, using the Mailgun service. Of course, you develop the API so that other developers use it, and you perfectly understand that a third-party developer may want to send it through other services, for example, the same mailchimp. Your task is to provide a clear "interface" to third-party developers. For example, each new provider must implement the setSubject, setBody, send methods, arguments to them , and you also indicate in the documentation what should be returnedthese methods. That's it, you've prepared the "interface". Now I am implementing it. What properties I will create there - this no longer concerns the interface. Yes, at least use the global variable, it is no longer his business. So think about whether abstract properties are needed at all
To answer why, I would answer that class properties can only take values of elementary data types and cannot have implementations in the form of a function as such.
And what is the reason to declare it abstract, that is, not implemented? It simply cannot be such by definition.
A property is an elementary element - a class description. An abstract method is a method that must be implemented in descendants. In an abstract class, it has no implementation (body). An abstract class is a class for which you cannot create an instance, you can inherit from it and implement it.
Usually, interfaces are used for abstract base classes, in which methods and properties are described. Descendant classes inherit these properties, implement methods - this simplifies the description of classes and standardizes their structural direction.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question