Jump to content

[SOLVED] Quick superglobal array question


KevinM1

Recommended Posts

This has something to do with my last thread with sessions.

 

I'm currently trying my hand at a Front Controller/Registry combo.  Nothing complex, just an exercise for me to learn the basic concepts.

 

I have a Request object, which is primarily an object wrapper for the $_REQUEST superglobal array.  The Request object works fine, with the exception being a notice thrown when error_reporting(E_ALL) is set.  That notice is:

Notice: Undefined index: action in /home/nights/www/www/MP/controller/Request.php5 on line 27

 

The relevant code is:

<?php
   class MP_controller_Request{
      private $properties;

      function __construct(){
         $this->init();
         MP_base_RequestRegistry::getInstance();
         MP_base_RequestRegistry::setRequest($this);
      }

      function init(){
         if($_SERVER['REQUEST_METHOD']){
            $this->properties = $_REQUEST;
            return;
         }

         foreach($_SERVER['argv'] as $arg){ //just in case the command line is used
            if(strpos($arg, '=')){
               list($key, $value) = explode('=', $arg);
               $this->setProperty($key, $value);
            }
         }
      }

      function getProperty($key){
         if(!empty($this->properties)){
            return $this->properties[$key]; // <-- line giving the notice
         }
      }

      function setProperty($key, $value){
         $this->properties[$key] = $value;
      }
   }
?>

 

The notice is only thrown before a request is sent to the Front Controller (i.e., on initial page load).  So, my question is this: does $_REQUEST exist even if a request isn't sent?  Because the notice makes it seem as though my code is trying to access the $this->properties array despite it logically being empty.

Link to comment
Share on other sites

Id' change this:

      function getProperty($key){
         if(!empty($this->properties)){
            return $this->properties[$key]; // <-- line giving the notice
         }
      }

to:

      function getProperty($key)
      {
         if(is_array($this->properties) && isset($this->properties[$key]))
         {
            return $this->properties[$key];
         }
      }[/code
Do note that the $_REQUEST superglobal contains both $_POST, $_GET and $_COOKIE data.

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.