PHP Variable Variables - php

How does php handle something like this...
$blah = "Testing a variable";
$$blah = "test";
What would my new variable name be?

Everything you need to know about variable variables at http://www.php.net/manual/en/language.variables.variable.php, except for one thing: don't use them.

echo ${'Testing a variable'};
However, you don't want to do this in practice. It makes for unmaintainable, bug-prone code.

The variable $blah must contain a valid variable name.
This will tell you about variables: http://www.php.net/manual/en/language.variables.basics.php

Not really an answer, but...
<?php
function I_love_you()
{
return "haha";
}
$haha = "HoHoHo";
$tom = "I_love_you";
$blah = "tom";
echo ${$$blah()};
?>

Related

Create a property of object with the value of variable [duplicate]

How do i dynamically assign a name to a php object?
for example how would i assign a object to a var that is the id of the db row that i am using to create objects.
for example
$<idnum>= new object();
where idnum is the id from my database.
You can use the a double dollar sign to create a variable with the name of the value of another one for example:
$idnum = "myVar";
$$idnum = new object(); // This is equivalent to $myVar = new object();
But make sure if you really need to do that, your code can get really messy if you don't have enough care or you abuse of using this "feature"...
I think you can better use arrays or hash tables rather than polluting the global namespace with dynamically created variables.
this little snippet works for me
$num=500;
${"id$num"} = 1234;
echo $id500;
basically just use the curly brackets to surround your variable name and prepend a $;
You can do something like this:
${"test123"} = "hello";
echo $test123; //will echo "hello"
$foo = "mystring";
${$foo} = "a value";
echo $mystring; //will echo "a value";
http://us2.php.net/language.variables.variable

PHP object properties

I'm new to OOP in PHP and I find the difference between the following two expressions difficult to understand.
$object->$foo;
$object->foo;
Maybe it's my fault, but I could not find the relevant part in the manual.
The first call $obj->$foo is using a so called variable variable. Check this:
class A {
public $foo = 1;
}
$a = new A();
$foo = 'foo';
// now you can use both
echo $a->$foo;
echo $a->foo;
Follow the manual about variable variables
Well, in order to fully understand the somewhat odd-looking $object->$foo, you should understand two things about PHP:
Variable names
Most of the time variables in PHP are quite straight-forward. They begin with a $ sign, have one [a-zA-Z_] character, and then any amount of [a-z-A-Z0-9_] characters. Examples include:
$var = 'Abcdef';
$_GET = [];
$a1 = 123;
// And so on...
Now, PHP variables can actually be named pretty much anything, as long as the name is, or can be cast to, a scalar type. The way you name a variable with anything is to use curly braces ({}), like this:
${null} = 'It works'; echo ${null};
${false} = 'It works'; echo ${false};
${'!'} = 'It works'; echo ${'!'};
// Slightly weirder...
${(int)trim(' 5 ')} = 'It works'; echo ${5};
${implode(['a','b','c'])} = 'It works'; echo $abc;
Important: Just because you can do this does not mean you should, however. It is mostly just an oddity of PHP that you can do this.
Variable variables
A somewhat convoluted explanation: A variable variable is a variable that is accessed using a variable name.
A much easier way to understand variable variables is to use what we just learning about variable names in PHP. Take this example:
${"abc"} = 'Abc...';
echo $abc;
We create a variable using the string, "abc", which can also be accessed using $abc.
Now, there is no reason (or rule) that says it has to be a string.... it can also be a variable:
$abc = 'Abc...';
$varName = 'abc';
echo ${$varName}; // echo $abc
That is basically a variable variable. "Real" variable variables just do not use the curly braces:
$abc = 'Abc...';
$varName = 'abc';
echo $$varName; // echo $abc
As for the question
In the question the $object->$foo thing is basically just an "object variable variable", if you like
$object = new stdClass;
$object->abc = 'The alphabet!';
$foo = 'abc';
echo $object->$foo;
echo $object->{$foo}; // The same
echo $object->{'abc'}; // The same
Object variable variables can be somewhat useful, but they are rarely necessary. Using an associative array is usually a better choice.

PHP - Concatenate object class name

Is is possible to concatenate an object's name?
The below doesn't seem to work..
Trying to call $node->field_presenter_en;
$lang = 'en';
$node->field_presenter_.$lang;
${$node->field_presenter_.$lang};
Thanks!
Try:
$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
This is called variable variables. More information here:
http://php.net/manual/en/language.variables.variable.php
Edit:
The user nickb has suggested a much more elegant solution below, and I will incorporate into this answer for easier reading (nickb: please let me know if you want me to remove this):
$node->{'field_presenter_'.$lang}
$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
<?php
class A {
public $prop = 'hello';
}
$a = new A();
echo $a->{'pro' . 'p'}; // hello

Using array value with index as Variable Variable

The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];

Dynamic Naming of PHP Objects

How do i dynamically assign a name to a php object?
for example how would i assign a object to a var that is the id of the db row that i am using to create objects.
for example
$<idnum>= new object();
where idnum is the id from my database.
You can use the a double dollar sign to create a variable with the name of the value of another one for example:
$idnum = "myVar";
$$idnum = new object(); // This is equivalent to $myVar = new object();
But make sure if you really need to do that, your code can get really messy if you don't have enough care or you abuse of using this "feature"...
I think you can better use arrays or hash tables rather than polluting the global namespace with dynamically created variables.
this little snippet works for me
$num=500;
${"id$num"} = 1234;
echo $id500;
basically just use the curly brackets to surround your variable name and prepend a $;
You can do something like this:
${"test123"} = "hello";
echo $test123; //will echo "hello"
$foo = "mystring";
${$foo} = "a value";
echo $mystring; //will echo "a value";
http://us2.php.net/language.variables.variable

Categories