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
https://forums.phpfreaks.com/topic/99999-solved-quick-superglobal-array-question/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.