r/PHPhelp • u/Mastodont_XXX • Dec 13 '25
Can implementation be cascaded?
Is there any way to enforce that a certain property must be overwritten in all derived classes?
Let's say I have this hierarchy:
abstract class BaseLevel
class FirstLevel extends BaseLevel
class SecondLevel extends FirstLevel
And I want to enforce that the property defined in BaseLevel must also be implemented (overwritten with a new value) in SecondLevel. I've tried probably everything, even interface, but implementation can only be enforced in FirstLevel, not higher. Because if I omit the implementation in SecondLevel, it is simply taken from FirstLevel.( And I would like to trigger a fatal error instead.)
2
u/MartinMystikJonas Dec 13 '25
You cannot force that on language level. But you can make custom rule for thet in PHPStan.
2
u/eurosat7 Dec 13 '25
Additional info: You can make the base class having `abstract` methods and define that all classes that are inherited are `final` and so you disallow any multi-level inheritance. That would technically achieve the same.
I am curious: Why do you need multi layer inheritance? Please explain. Because maybe you want to use more `interfaces` and use "composition over inheritance".
If you have any code to share between classes you can extract that into services and inject them in the constructor - or go the old school way of using traits, but that is often discouraged today.
8
u/BaronOfTheVoid Dec 13 '25
Instead of trying to fool with properties directly just do
Obviously replace PropertyName and type with whatever you want to call it and whatever type it is supposed to be.
3
u/obstreperous_troll Dec 13 '25
If it's implemented in FirstLevel, then it's implemented in SecondLevel, that's how inheritance is meant to work. If you need to enforce further invariants, put them in the constructor.
3
u/FreeLogicGate Dec 14 '25
Sounds like an XY problem. Rather than concentrate on your perceived solution, you would probably have better luck describing the problem you are trying to solve. In general, any concentration on properties is likely misguided given the standard OOP principles of Encapsulation and Information Hiding.
1
u/equilni Dec 13 '25 edited Dec 14 '25
Unless I am misunderstanding, can you use property hooks here?
https://3v4l.org/Tnb34#v8.4.15
1
u/HolyGonzo Dec 13 '25 edited Dec 13 '25
This is probably not a good idea, but you could use Reflection to check the current class in the constructor, get the properties, and see if certain ones were declared by the current class. If not, then you can throw an exception. It won't find problems unless you actually create an instance, and if you had to create a constructor for a custom class, you'd need to ensure you called the parent constructor, too.
Because it's using reflection on every instance, it cuts the class performance down to about 10%. Seems more like it would be better to maybe do this kind of thing in a unit test suite or something rather than in the class code, though.
1
u/23creative Dec 13 '25
Can you force the values null on the __construct? Or compare “self” and “static” values in your method
1
u/HolyGonzo Dec 13 '25
Just following up on my last comment. Here's an example where you could do it in a separate unit-test type of way, instead of trying to incorporate the logic directly into the class itself.
The result is:
1
u/HolyGonzo Dec 13 '25
Also, I'm assuming you needed something that was public scope? Otherwise you could just use private scope, which isn't inherited.