NotionCommotion Posted August 22, 2016 Share Posted August 22, 2016 I have the following lines of code, and about 50 more similar ones which respond to different requests. Many of them just pass ['logger'=>$this->logger,'db'=>$this->db,'account'=>$this->account] to the class, but some pass a couple more parameters. $app->get('/configure', function (Request $request, Response $response) { $configure=new \MyApp\Configure(['logger'=>$this->logger,'db'=>$this->db,'account'=>$this->account]); return $response->withJson($configure->get()); }); I would like to type less regarding the the array passed to the class. While the following doesn't work, I hope it better conveys what I am trying to accomplish. Any suggestions? Thank you $get=function($extra=[]) { return array_merge($extra,['logger'=>$this->logger,'db'=>$this->db,'account'=>$this->account]); }; $app->get('/configure', function (Request $request, Response $response) { $configure=new \MyApp\Configure($get()); return $response->withJson($configure->get()); }); $app->get('/oneThatNeedsMore', function (Request $request, Response $response) { $configure=new \MyApp\Configure($get(['extra'=>123])); return $response->withJson($configure->foo()); }); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 22, 2016 Share Posted August 22, 2016 Not an OOP guy by any means, but I have to wonder why your object can't contain an 'array' to which you pass this array? It appears that var x is going to $obj=>x so why not just pass the whole array to $obj=>array? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 22, 2016 Share Posted August 22, 2016 What is Configure? And why does it need extra parameters when it's always the same class? Right now, it seems a much reasonable approach would be to pass $this (which is appearently the Slim application) and possibly other data sources like $request and $response to the class and let it decide which data is needed. If you need extra data for a specific method, use method parameters. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted August 23, 2016 Author Share Posted August 23, 2016 What is Configure? And why does it need extra parameters when it's always the same class? Right now, it seems a much reasonable approach would be to pass $this (which is appearently the Slim application) and possibly other data sources like $request and $response to the class and let it decide which data is needed. If you need extra data for a specific method, use method parameters. Configure is a class to set the configuration values. You are correct, it doesn't need any extra parameters, but I have other classes which do. Yes, passing the Slim application will work, but maybe gives too much. Quote Link to comment Share on other sites More sharing options...
kicken Posted August 23, 2016 Share Posted August 23, 2016 I wouldn't worry about just passing $this possibly providing access to too much. So long as your target class only deals with what it needs it doesn't really matter. Something you could do is create a function that returns your closure and does the parameter merging there. For example: function Controller($controller, $extra=[]){ return function(Request $request, Response $response){ $params = array_merge($extra, ['logger'=>$this->logger, 'db'=>$this->db, 'account'=>$this->account]); $controller = new $controller($params); return $response->withJson($controller->get()); }; } $app->get('/configure', Controller(\MyApp\Configure::class)); $app->get('/oneThatNeedsMore', Controller(\MyApp\Configure::class, ['extra'=>123])); Passing the class name would allow you to use different classes provided they all used the same structure. Alternatively you could pass an instance of a class that uses a specific interface and call a method to set the parameters / app object. interface ControllerInterface { public function setApplication($app); public function setParameters($params); public function get(Request $request); } abstract class Controller implements ControllerInterface { protected $app; protected $params = []; public function setApplication($app){ $this->app = $app; } public function setParams($params){ $this->params = $params; } } class Configure extends Controller(){ public function get(Request $request){ $logger = $this->app->logger; $db = $this->app->db; $account = $this->app->account; } } function Controller(ControllerInterface $controller, $extra=[]){ return function(Request $request, Response $response){ $controller->setApplication($this); $controller->setParams($extra); return $response->withJson($controller->get($request)); }; } $app->get('/configure', Controller(new Configure)); $app->get('/oneThatNeedsMore', Controller(new Configure, ['extra'=>123])); If you take some time to think about what you need to do and how to do it, you could come up with some better code based on your requirements. For example, maybe pass those extra parameters as constructor arguments instead. Maybe have the controller alias some useful services so you can just do $this->db instead of $this->app->db in your methods. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted August 23, 2016 Author Share Posted August 23, 2016 Thanks Kicken, Will need to mull this over a bit. One initial question, however. Please explain \MyApp\Configure::class. $app->get('/configure', Controller(\MyApp\Configure::class)); Quote Link to comment Share on other sites More sharing options...
kicken Posted August 23, 2016 Share Posted August 23, 2016 (edited) Manual page ::class Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes. Edited August 23, 2016 by kicken Quote Link to comment 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.