F
F
froosty2015-07-03 11:34:22
PHP
froosty, 2015-07-03 11:34:22

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

5 answer(s)
K
kompi, 2015-07-03
@kompi

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.

D
Danil Chekalin, 2015-07-03
@dakiesse

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

M
Melkij, 2015-07-03
@melkij

PHP Fatal error: Properties cannot be declared abstract in

D
Danila, 2015-07-03
Ridzhi @Ridzhi

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.

A
Alexey, 2015-07-03
@HaJIuBauKa

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 question

Ask a Question

731 491 924 answers to any question