Dynamic naming of PHP objects

How do I dynamically assign a name to a php object?

For example, how would I assign a var object, which is the identifier of the db string that I use to create the objects.

eg

$<idnum>= new object();

where idnum is the identifier from my database.

+3
source share
4 answers

this little snippet works for me

$num=500;
${"id$num"} = 1234;
echo $id500;

basically just use curly braces to surround the variable name and add $;

+5
source

You can use the double dollar sign to create a variable with the value name of another, for example:

$idnum = "myVar";

$$idnum = new object(); // This is equivalent to $myVar = new object();

, , , ""...

, -, .

+13

You can do something like this:

${"test123"} = "hello";
echo $test123; //will echo "hello"

$foo = "mystring";
${$foo} = "a value";
echo $mystring; //will echo "a value";
+4
source

Source: https://habr.com/ru/post/1702227/


All Articles