NotionCommotion Posted October 22, 2015 Share Posted October 22, 2015 (edited) I have three classes which each have their own method "pm()". Each of them call mainMethod(), and pass $a and $b. All of them also need to pass a value for x and y except for one that also needs to pass a value for z, and as such, I was planning on passing an array for these "extra" values. mainMethod is the same for all of them except one small section. It is possible that mainMethod() will be called by more than once in a given class and will require different functionallity for this "small section". Originally, I planned on having the pm() methods pass mainMethod() an anonymous function, but have since learned the error of my ways. I recognize I am not providing "all the information", but am hoping that I have provided enough to get advice whether I am doing this the right way. Thanks <?php class class1 extends mainClass { public function pm() { //some script $extra=['x'=>1,'y'=>2]; $this->mainMethod($a,$b,'pm_callback',$extra); } protected function pm_callback($extra,$n,$m) { //some script that uses $this->model, $extra, $n, and $m } } class class2 extends mainClass { public function pm() { //some script $extra=['x'=>1,'y'=>2]; $this->mainMethod($a,$b,'pm_callback',$extra); } protected function pm_callback($extra,$n,$m) { //some script that uses $this->model, $extra, $n, and $m } } class class3 extends mainClass { public function pm() { //some script $extra=['x'=>1,'y'=>2,'z'=>3]; $this->mainMethod($a,$b,'pm_callback',$extra); } protected function pm_callback($extra,$n,$m) { //some script that uses $this->model, $extra, $n, and $m } } class mainClass { protected function mainMethod($a,$b,$callback,$extra) { $model=new model(); //some script $this->$callback($extra,$n,$m); //some script } } ?> Edited October 22, 2015 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/298773-modifying-a-method-common-to-multiple-classes/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.