Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 09:56 PM   PM User | #1
anotherJEK
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: 152
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
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
anotherJEK is offline   Reply With Quote
Old 02-18-2013, 12:44 AM   PM User | #2
anotherJEK
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: 152
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
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?
anotherJEK is offline   Reply With Quote
Old 02-18-2013, 02:13 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-18-2013, 09:03 PM   PM User | #4
anotherJEK
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: 152
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
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
anotherJEK is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:58 PM.


Advertisement
Log in to turn off these ads.