Answer the question
In order to leave comments, you need to log in
Php trait overriding magic methods?
The essence of the question is in the title.
Is it possible and how to override the __construct __destruct magic methods in a class using the trait ?
for example, so that when the class dies, the class method is called first, then the method from the trait.
I didn't find it in the manual.
Thank you.
Answer the question
In order to leave comments, you need to log in
What is there to look for in the manual?
It's faster to write code and test.
You can use the magic method from traits.
They will be called if the class does not have its magic methods specified.
Class methods are called first, so overriding without changing the class will not work. Those. in class methods, you need to manually call the method from the trait.
In Google, right now, a bunch of everything was found for some reason.
When using a trait, you can reassign the method name: php.net/manual/ru/language.oop5.traits.php#languag...
Thus, you can include a method __construct
with a different name and call it at the end of the class constructor. Likewise with any other methods. PHP does nothing automatically in this regard, all conflicts must be resolved explicitly, otherwise they will lead to an error.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question