Results 1 to 3 of 3

Thread: variables to class

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default variables to class

    i have a page which has some get variables and i include class file.
    what is the best way to get the variables from the page into the class?

    my page:
    Code:
    $myPage = $_GET['p'];
    $userID = $_GET['id'];
    require_once("./class/UploadHandler.php");
    my class, UploadHandler.php:
    Code:
    class UploadHandler
    {
        function __construct($options = null) {
    		$path = './../' . $userID . '/'. $myPage;
        }
    }
    Last edited by ggalan; 11-24-2011 at 01:38 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    <?php
    class UploadHandler{
       public function 
    __construct($options=null){
          global 
    $myPage,$userID;
          
    // ...
       
    }

    // OR pass them into the class when initialized //
       
    public function __construct($options=null,$variables_from_global_scope){
          
    $myPage $variables_from_global_scope['myPage'];
          
    $userID $variables_from_global_scope['userID'];
          
    // ...
       
    }

    //  OR simply  //
       
    public function __construct($options=null){
          
    $myPage $_GET['p'];
          
    $userID $_GET['id'];
          
    // ...
       
    }
    }

  3. The Following User Says Thank You to traq For This Useful Post:

    ggalan (11-24-2011)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Just to add some info: traq's last option is the clear answer because $_GET (and others like it) is a variable without scope-- you can always access it. So there's no difference in using that for a class instead of in the regular non-class code.

    But if you ever need to get a variable you can use the global method as in traq's first example, or use this convenient trick:
    $GLOBALS['any_variable_name']; //equivalent to $any_variable_name in global scope

    The only other way to do it is as in traq's second example, passing the values directly to the class (via a function or the class instance creation line).

    Finally, remember that these methods have different effects for whether the value is saved to the variable if you change it. If you pass a variable to the function or otherwise make a copy, it won't change it's global value. But if you use a global method it will change it throughout the entire script.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •