php - return type of container from subclass -
I have a PHP library that I do not want to edit, and implementing my code by override / override some methods is. But I'm stuck with the sanity. For example:
Expand the square moment PHHP moment {public $ unique settings; Public function format js () {return original :: format ($ this- & gt; unique settings); }} Class paint {public function startOf () {// some code returns $ $; }}
I want to do this:
$ momentphp = new moment PHH (); $ DateStart = $ momentphp-> Start ('month') - & gt; FormatJs ();
And to return to yourself the way to do this, Moment is overriding all the child's methods within PHHP
Is there any other easy way to do this? The way? Using _call
or something else?
Edit: found a way to do this:
- Remove
- Switch between classes Use the
__ call
method for.
> Like this:
Class Moment PHH {Personal $ Example = Zero; Public $ unique settings; Public function __ composition () {$ this- & gt; Example = new moment (); } Public function __call ($ method, $ args) {if (in_array ($ method, get_class_methods ($ this))) {call_user_func (array ($ this, $ method), $ args); And call_user_func (array ($ - example- $, $ method), $ args); $ This return; } Public function format js () {$ $ - this- & gt; Example- & gt; Format ($ -> - Unique Settings); }} Class paint {public function startOf () {// some code returns $ $; }}
Is there a better way?
There is a proper way to do this:
Class Moment PHH {Private $ example = zero; Public $ unique settings; Public function __ composition () {$ this- & gt; Example = new moment (); // settings etc} public function __call ($ method, $ args) {$ result = NULL; If (in_array ($ method, get_class_methods ($ this))) {$ result = call_user_func (array ($ this, $ method), $ args); Else $ result = call_user_func (array ($ this-> example, $ method), $ args); If (example of a $ result for the moment) $ this- & gt; Example = $ results; $ This return; } Public function format () {$ $ - this-> Example- & gt; Format ($ this-> unique settings); }}
Updating examples from the result of the method is important, and instead of $ this->
$ this
Using> Allows you to use the filling square in every call, so you can override the function using chain of other methods of parent class with chain capacity.
Comments
Post a Comment