Jump to content

Recommended Posts

Hello All,

 

I am fairly new to PHP class development, and I was wondering if it is normal behaviour to see classes not being able to access global variables related to PHP-based requests ($_GET, $_SESSION, etc.)? I seem to either have to use "global <var>" inside of the class to access the data, or I am forced to change the methods so then such required data is passed in as parameters. Perhaps I am just doing something wrong?

 

Here is an example of what I mean:

class Something
{
       function aFunc()
       {
               if (isset($_SESSION['somedata'])) { return false; } // this always returns false whether I had set the value or not
       }
       function bFunc()
       {
              global $_SESSION; // figured this would already be in a global accessible scope
              if (isset($_SESSION['somedata'])) { return false; } // now this will return false only when the value is set
       }
}

 

Now the above example is commented to demonstrate what I was meaning above, and I was wondering if this was the 'norm' when developing classes for PHP (and thus such values should be passed as parameters)?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/213726-classes/
Share on other sites

ooops the above example should be (sorry I typed it up really quick the first time):

 

class Something
{
       function aFunc()
       {
               // this always returns true whether I had set the value or not
               if (isset($_SESSION['somedata'])) { return false; }
               return true;
       }
       function bFunc()
       {
              global $_SESSION; // figured this would already be in a global accessible scope
              if (isset($_SESSION['somedata'])) { return false; } // now this will return false only when the value is set
              return true;
       }
}

 

There, hopefully that makes a little more sense.

Link to comment
https://forums.phpfreaks.com/topic/213726-classes/#findComment-1112453
Share on other sites

A) You should always post actual code that exhibits the stated symptom so that someone can actually help you (there's no reason to be typing code unless it is in a reply trying to help someone.)

 

B) The symptom you are experiencing is that of a session_start() not working (or not even present) and the $_SESSION variable is a plain program variable and not the actual session array. Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed to help you find out why it is not working as expected?

 

C) Using global on one of the actual super-global arrays actually causes a new variable by that name to be created inside of the function/class and it is NOT a copy of the actual super-global and does not have the values of the super-global array.

 

D) Don't use global any way as it breaks the black-box nature of functions/classes so that they cannot be reused without keeping track of the main program variables that are hard coded into them.

 

E) "and thus such values should be passed as parameters" Yes, that's the way you should always write functions/classes.

Link to comment
https://forums.phpfreaks.com/topic/213726-classes/#findComment-1112493
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.