Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    213
    Thanks
    52
    Thanked 1 Time in 1 Post

    Help with an essoteric syntax

    Some time ago I got help from this forum on how to
    convert a string to a variable in php.

    Essentially, I have an associative array that contains array names as strings.
    I want to get to the values contained in arrays as strings. So I need to
    convert the string rep of the array to the actual array variable.
    PHP Code:
     ${$_arrayNames['_arrayX']} ['Acrylics']; 
    Here $_arrayNames contains the string representation of an array name with
    the value 'Acrylics'. So I want the value in $_arrayX['Acrylics']
    This does work. BUT.....
    in OOP context:
    PHP Code:
    $this->depts self::$_arrayNames['departments']; // string '_deptDefaults'
    print ${$this->depts}['hm']; // nada
    require_once(${$this->depts}['hm']); // Fatal error.... etc...
    /// ..etc... 
    The ultimate value is supposed to be a file name with path as the value
    for $_deptDefaults['hm']
    require_once() is failing

    SO.... any suggestions about how to get this to work?

    (as I look over this I think I know why it isn't working... there is not a record in the object, of the arrays that are being referenced in arrayNames)

    Well, thanks for time and attention

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    213
    Thanks
    52
    Thanked 1 Time in 1 Post

    I got it working

    But I decided to piggy back another similar question:
    PHP Code:
    $_functList = array();
    $_functList[''] = "genIntro";
    $_functList['em'] = "genForm";
    $_functList['sub'] = "contactResult";
    $_functList['test'] = "testFunction";

    $_do = {$_functList['test']}(); // Parse error: syntax error, unexpected '{' in  (path)/contact.php on line 34 (this is line 34)

    function testFunction()
             {
              print 
    "Hello???.....";
              return;
             } 
    IE: how to apply the same conversion idea to a function name???
    .... or can I do anonymous functions, like in javascript?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    For the first one, what is the scope of the $_deptDefaults array? Is it declared within the method? Variable variables would require something to see. Enable the error reporting to see if it cannot find the variable or offsets specified.

    The second one doesn't require the use of variable variables. A simple $_funcList['test'](); does what you want. Variable functions are not the same as invoking a string, so therefore you do not want to end up with 'string'().
    To create an anonymous function, you can use the create_function syntax or invoke the closure:
    PHP Code:
    $func create_function('''print "Hello???....."; return false;');
    $func = function(){
        print 
    "Hello???. . . . .";
        return 
    false;
    }; 
    The latter requires 5.3+.

    As for your actual use of variable variables, these are debugging nightmares. Fortunately, there is only one situation I've run into that requires them, and that is using a completely dynamic bind_param on the mysqli library since it doesn't accept either values nor one bind at a time. Other than that, I've never found a block that requires the use of variable variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    213
    Thanks
    52
    Thanked 1 Time in 1 Post

    Found something

    for the first question, I did not actually have the arrays at the other end of the reference chain
    referenced in the object method so ${$_someArray['x']} didn't actually point to anything.

    on the second question
    I was experimenting around and came up with a way:
    PHP Code:
    $_functList = array()
    $_functList['test'] = "testFunction";
    function 
    testFunction()
             {
              print 
    "Hello???.....";
              return;
             }
    $_do $_functList['test'](); // ***BINGO***
    //// *** but suggests a liability for inadvertantly calling executable code. 
    I am running php v5.2x

    The whole point of this exercise is that I am doing a web site and want to
    make a site that is essentially one page that processes itself. The content is
    based on $_GET queries appended to internal links. The page is completely
    self processing, including a contact form that uses post method. So,
    the code has to decide what do do with get and post vars without knowing
    before hand exactly what the user will request. Particularly, the contact form
    has several phases based on the result of executing a captcha graphic screen
    process.
    Last edited by anotherJEK; 02-18-2013 at 09:09 PM. Reason: retrospect


  •  

    Posting Permissions

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