Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 993135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:22:21+00:00 2026-05-16T06:22:21+00:00

So, I don’t come from a huge PHP background—and I was wondering if in

  • 0

So, I don’t come from a huge PHP background—and I was wondering if in well formed code, one should use the ‘superglobals’ directly, e.g. in the middle of some function say $_SESSION['x'] = 'y'; or if, like I’d normally do with variables, it’s better to send them as arguments that can be used from there, e.g:

class Doer {
    private $sess;
    public function __construct(&$sess) {
        $this->sess =& $sess;
    }
} 

$doer = new Doer($_SESSION);

and then use the Doer->sess version from within Doer and such. (The advantage of this method is that it makes clear that Doer uses $_SESSION.)

What’s the accepted PHP design approach for this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:22:22+00:00Added an answer on May 16, 2026 at 6:22 am

    I do like to wrap $_SESSION, $_POST, $_GET, and $_COOKIE into OOP structures.

    I use this method to centralize code that handles sanitation and validation, all of the necessary isset () checks, nonces, setcookie parameters, etc. It also allows client code to be more readable (and gives me the illusion that it’s more maintainable).

    It may be difficult to enforce use of this kind of structure, especially if there are multiple coders. With $_GET, $_POST, and $_COOKIE (I believe), your initialization code can copy the data, then destroy the superglobal. Maybe a clever destructor could make this possible with $_SESSION (wipe $_SESSION on load, write it back in the destructor), though I haven’t tried.

    I don’t usually use any of these enforcement techniques, though. After getting used to it, seeing $_SESSION in code outside the session class just looks strange, and I mostly work solo.

    EDIT
    Here’s some sample client code, in case it helps somebody. I’m sure looking at any of the major frameworks would give you better ideas…

    $post = Post::load ();  
    $post->numeric ('member_age');  
    $post->email ('member_email');
    $post->match ('/regex/','member_field');
    $post->required ('member_first_name','member_email');
    $post->inSet ('member_status',array('unemployed','retired','part-time','full-time'));
    $post->money ('member_salary');
    $post->register ('member_last_name'); // no specific requirements, but we want access
    if ($post->isValid())
    {
      // do good stuff
      $firstName = $post->member_first_name;
    }
    else
    {
      // do error stuff
    }
    

    Post and its friends all derive from a base class that implements the core validation code, adding their own specific functionality like form tokens, session cookie configuration, whatever.

    Internally, the class holds a collection of valid data that’s extracted from $_POST as the validation methods are called, then returns them as properties using a magic __get method. Failed fields can’t be accessed this way. My validation methods (except required) don’t fail on empty fields, and many of them use func_get_args to allow them to operate on multiple fields at once. Some of the methods (like money) automatically translate the data into custom value types.

    In the error case, I have a way to transform the data into a format that can be saved in the session and used to pre-populate the form and highlight errors after redirecting to the original form.

    One way to improve on this would be to store the validation info in a Form class that’s used to render the form and power client-side validation, as well as cleaning the data after submission.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't be scared of the extensive code. The problem is general. I just provided
Don't really know how to formulate the title, but it should be pretty obvious
Don't think that I'm mad, I understand how php works! That being said. I
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
Don't understand, if Data.Map is and [] is. I found this out while wondering
I don't like Jackson. I want to use ajax but with Google Gson. So
I don't have much PHP experience and I want to know how to best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.