SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Location
    Dundee, Scotland
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with variable variables.

    Hi all

    Having a problem with variable variables at the moment which I can't seem to fathom.

    As a simple example I have the following:-

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    $meth 'GET';
    $method '_'.$meth;
    print_r(${$method});
    class 
    test
    {
        var 
    $meth 'GET';
        function 
    test()
        {
            
    $method '_'.$this->meth;
            
    print_r(${$method});
        }
    }
    $a = new test;
    ?>
    As you can see I am doing the same thing within the class as I am doing outside of it. The problem is however that the code outside the class does as expected and prints out the content of the $_GET global array. However the code within the class results in a notice which it:-

    Notice: Undefined variable: _GET in /home/domain/public_html/test/test.php on line 12
    The reason I am trying to do this is because I am making a class for handling forms but it can be set whether I use post or get. Therefore I thought of using variable variables to retrieve the data from the $_POST or $_GET array (whichever is relevant). I suppose I could use $_REQUEST but I would prefer to stay away from that.

    Can anyone suggest a reason why this will not work within a class?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Couldn't you just do something like

    PHP Code:
    $data count($_POST) ? $_POST $_GET
    unless you're passing $_GET and $_POST as the same time.

  3. #3
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with wonkishee... you're going five times 'round the block to get across the street.
    PHP Code:
    <?php

    class FormMagik {
        var 
    $data;
        
        
    /* you'd use __construct if you're on php5 */
        
    function FormMagik ($method='GET') {
            
    $method=='GET' $this->data=$_GET:$this->data=$_POST;
        }
        
        function 
    debugData() {
            
    printf("<pre>%s</pre>",print_r($this->data,1));
        }
    }

    $fm = new FormMagik('POST');
    $fm->debugData();
    ?>

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by manual
    Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.
    If you're asking for the reason... well, it's the fault of Zend engine. However, as others shown, this construct is actually not needed.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2006
    Location
    Dundee, Scotland
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers guys I will look at doing it that way.

    stereofrog I did not notice the note in the manual. Seemed strange that a global array couldn't be reached by this method in a class.


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
  •