Jump to content

Undefined index error where it should not be


Codin2012
Go to solution Solved by ginerjm,

Recommended Posts

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 by cyberRobot
added [code][/code] tags
Link to comment
Share on other sites

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 by cyberRobot
removed the duplicate quote
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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'];
		}
	  }
	}
Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.