1

I'm learning php and I ran into the following head-scratcher:

I have 2 objects, let's say

class Fruit {
public $weight;
}

$apple = new Fruit();
$apple->weight = 1;

$banana = new Fruit();
$banana->weight = 2;

Later on, from I get some input from the user as a variable (like, which fruit do you like best?):

$user_preference =  'apple';

Now, how can I reference the correct object dynamically? How to get something like

echo $user_preference->weight; 

?

2
  • echo ${$user_preference}->weight; or echo $$user_preference->weight;. See variable variables. It's not exactly a recommended feature to use in most cases though. Commented Jan 8, 2020 at 14:42
  • Remember, if this is web based, when yo get the data from the user the original Objects will have been destroyed Commented Jan 8, 2020 at 14:43

2 Answers 2

4

I would create a map (usefull when data is retrieved from a db for example)

$apple = new Fruit();
$apple->weight = 1;

$banana = new Fruit();
$banana->weight = 2;

$fruitMap = ['apple'=>$apple,'banana'=>$banana];

$user_preference =  'apple';

echo $fruitMap[$user_preference]->weight;

But do a check if the key exists

Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely a safer way to go rather than using variable variables
1

You can use Variable variables :

<?php
class Fruit {
    public $weight;
}

$apple = new Fruit();
$apple->weight = 1;

$banana = new Fruit();
$banana->weight = 2;

$user_preference =  'apple';

//   vv---------------- Check this notation
echo $$user_preference->weight;  // outputs 1

Test it yourself


Note that this can lead to security breaches because

  1. Never trust user inputs.
  2. Never trust user inputs especially when it comes to control your code execution.
  3. Never trust user inputs.

Imagine you do echo $$user_input; and user input is database_password

To avoid it, you need to sanitize user inputs, in example :

<?php
class Fruit {
    public $weight;
}

$apple = new Fruit();
$apple->weight = 1;

$banana = new Fruit();
$banana->weight = 2;

$allowed_inputs = ['apple', 'banana'];

$user_preference =  'apple';

if (in_array($user_preference, $allowed_inputs))
{
    echo $$user_preference->weight;  // outputs 1
}
else
{
    echo "Nope ! You can't do that";
}

But this is at the cost of typing more code. ka_lin's solution is safer and easier to maintain

6 Comments

@Dilek why do you suggest that change? Isn't that the key to the answer, variable variables?
@Cid, could you elaborate a bit on the this can lead to security breaches please?
is it ? I didnt see it in example w3schools.com/php/php_oop_classes_objects.asp sorry will remove. and I market it as usefull.
@jibsteroos about security: when you use a User input as a variable name, user can enter name of a unexpected variable. imagine that.
This can lead to security breaches because 1) Never trust user inputs. 2) Never user inputs especially when it comes to control your code execution. imagine you do echo $$user_input; and user input is database_password
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.