Variable functions and variable names in PHP

In PHP, say you have code like this:

$infrastructure = mt_rand(0,100); if ($infrastructure < $min_infrastructure) $infrastructure = $min_infrastructure; //do some other stuff with $infrastructure $country->set_infrastructure($infrastructure); $education = mt_rand(0,100); if ($education < $min_education) $education = $min_education; //do some other stuff with $education $country->set_education($education); $healthcare = mt_rand(0,100); if ($healthcare < $min_healthcare) $healthcare = $min_healthcare; //do some other stuff with $healthcare $country->set_healthcare($healthcare); 

Is there a way to combine these similar sets of instructions into a function that can be called as:

 change_stats("infrastructure"); change_stats("education"); change_stats("healthcare"); 

Basically, can variables be used in PHP for other variable names and function names?

Thanks in advance.

+1
source share
5 answers

You can use what PHP calls "variable variables" for this. I hope your example is contrived, because it looks a little strange, but assuming the variables and objects are global, you can write the name_pet () function as follows:

 function name_pet($type, $name) { $class='the_'.$type; $var=$type.'_name'; $GLOBALS[$class]->setName($name); $GLOBALS[$var]=$name; } 

EDIT : This answer refers to an earlier version of the question.

+3
source

I'm not sure about this function, but you can do something like this using __set

 $data; function __set($key, $val) { $this->data["$key"] = $val; } 

And yes, you can use variables dynamically

 $foo = "bar"; $dynamic = "foo"; echo $$dynamic; //would output bar echo $dynamic; //would output foo 
0
source

Yes, you can, look here for a discussion and here for a tutorial.

0
source

To answer your question: Yes, you can use variables as variable names using the syntax $ {$ varname}.

However, this does not seem to be the right solution for what you are trying to do here, since setting $ _ _ {$ petname} variables requires them to be in the scope of the name_pet function.

Could you tell us more about what you are trying to do?

Some suggestions: Let the pet class (or something else that the cat, dog and fish) return the name to be set, so you can do $ fish_name = $ the_fish-> setName ("Goldie");

Or even better, do not use $ fish_name at all, as this information is now stored in the object ... you can simply call $ the_fish-> getName (); where you will use $ the_fish.

Hope this helps?

0
source

This is an interesting question because it is a common template and it is especially important to monitor refactoring.

In a purely functional form, you can use the following code:

 function rand_or_min( $value, $key, $country ) { $rand = mt_rand(0,100); if ($rand < $value ) { $rand = $value; } // do something call_user_func( array( $country, 'set_' . $value ), array( $rand ) ); } $arr = array('infrastructure' => 5,'education' => 3,'healthcare' => 80); array_walk( $arr, 'rand_or_min', $country ); 

Although this works well, I strongly recommend that you use a more object-oriented path. Whenever you see a pattern like the one above, you should think about classes and subclasses. What for? Because there is duplicate behavior and a similar state (variables).

In a more OOP method, this can be implemented as follows:

 class SomeBasicBehavior { function __construct( $min = 0 ) { $rand = mt_rand(0,100); if( $rand < $min ) { $rand = $min }; return $rand; } } class Infrastructure extends SomeBasicBehavior { } class Education extends SomeBasicBehavior { } class Healthcare extends SomeBasicBehavior { } $country->set_infrastructure( new Infrastructure() ); $country->set_education( new Education() }; $country->set_healthcare( new Healthcare() }; 

It is not only more readable, but also much more extensible and easily verifiable. Your β€œdo something” can be easily implemented as member functions in each class, and their behavior can be split as needed (using the SomeBasicBehavior class) or encapsulated as needed.

0
source

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


All Articles