Codin2012 Posted September 14, 2016 Share Posted September 14, 2016 (edited) I am trying to build an OOP PHP following MVC formatt. I do not understand why these lines of code would give an undefined index error. public function __construct($request){ $this->request = $request; if($this->request['controller'] == ""){ $this->controller = 'home'; } else { $this->controller = $this->request['controller']; } if($this->request['action'] == ""){ $this->action = 'index'; } else { $this->action = $this->request['action']; } } It is my two if statements causing the errors. All help greatly appreciated. Edited September 14, 2016 by cyberRobot added [code][/code] tags Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 14, 2016 Share Posted September 14, 2016 Have you tried outputting $request to see what it contains? public function __construct($request){ print '<pre>' . print_r($request, true) . '</pre>'; //... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 14, 2016 Share Posted September 14, 2016 Actually I would do that print of the input argument before calling the constructor, ie, before creating the object. Obviously the item you are passing is either not an array or not an array containing what you think it does. Quote Link to comment Share on other sites More sharing options...
Codin2012 Posted September 14, 2016 Author Share Posted September 14, 2016 Have you tried outputting $request to see what it contains? public function __construct($request){ print '<pre>' . print_r($request, true) . '</pre>'; //... Thank you for the suggestion I tried what you suggested and it was an empty array. Quote Link to comment Share on other sites More sharing options...
Codin2012 Posted September 14, 2016 Author Share Posted September 14, 2016 (edited) Actually I would do that print of the input argument before calling the constructor, ie, before creating the object. Obviously the item you are passing is either not an array or not an array containing what you think it does. Could you possibly give me an example of what you are suggesting? Thank you Edited September 14, 2016 by cyberRobot removed the duplicate quote Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted September 14, 2016 Solution Share Posted September 14, 2016 You apparently have produced this variable (a query result perhaps?) before trying to create the new object. But like so many people who are new to PHP (and/or programming) you fail to check the result of that query(?) to see if you have something in that array. Do that and you won't run into this situation. Or you could add some more code to the constructor to allow for an empty argument if that actually makes sense to do, ie, creating the object when you don't have the input for it. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 14, 2016 Share Posted September 14, 2016 Thank you for the suggestion I tried what you suggested and it was an empty array. Where does $result come from? We may be able to help if you show the code that creates the variable being passed to the class constructor. Quote Link to comment Share on other sites More sharing options...
Codin2012 Posted September 15, 2016 Author Share Posted September 15, 2016 You apparently have produced this variable (a query result perhaps?) before trying to create the new object. But like so many people who are new to PHP (and/or programming) you fail to check the result of that query(?) to see if you have something in that array. Do that and you won't run into this situation. Or you could add some more code to the constructor to allow for an empty argument if that actually makes sense to do, ie, creating the object when you don't have the input for it. As you pointed out I did not test to see, whether $request actually has a 'controller' entry. so this is what I did to solve the problem public function __construct($request){ $this->request = $request; if (isset($request['controller'])) { if ($this->request['controller'] == ""){ $this->controller = 'home'; } else { $this->controller = $this->request['controller']; } if($this->request['action'] == ""){ $this->action = 'index'; } else { $this->action = $this->request['action']; } } } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 15, 2016 Share Posted September 15, 2016 (edited) Did you read your code? Seems like a lot of un-needed lines. How about this: if (isset($request['controller']) if $request('controller'] == '' $request['controller'] = 'home'; if (isset($request['action']) if ($request['action'] == '') $request['action'] = 'index'; $this->request = $request; But of course both my logic and yours leaves out the test for either one of those elements existing and still being blank. Edited September 15, 2016 by ginerjm 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.