I am a newbie, if I encounter this, I can’t understand the beauty of it, record it;
The general logic is as follows
<code><span>abstract</span><span><span>class</span><span>A</span> {</span><span>private</span><span>static</span><span>$c</span>; <span>final</span><span><span>function</span><span>todo</span><span>()</span> {</span><span>//do something</span> } <span>public</span><span><span>function</span><span>init</span><span>()</span> {</span><span>$c</span> = <span>new</span> B; } <span>public</span><span><span>function</span><span>get</span><span>()</span> {</span><span>$c</span>->todo(); } } <span><span>class</span><span>B</span><span>extends</span><span>A</span> {</span>} .... A::get();</code>
Several questions arise here:
1. Can I create a new subclass in a parent class?
2. What are the considerations for doing this? Or what kind of problems is this design pattern avoiding?
The above has introduced the abstract class in PHP that instantiates its subclass in its method to complete the call of its own method, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.