Reputation: 434
$pet ='dog';
$action='My '. $pet . 'likes to run.';
//The part I would like to modify
$pet ='cat';
//Modify the $pet variable inside the $action variable
//after it has been defined.
echo $action;
This will output: My dog likes to run
Can I make it output: My cat likes to run
I also tried:
$pet ='dog';
$action=sprintf('My %s likes to run.', $pet);
$pet ='cat';
echo $action;
I do know that this could be achieved by creating a function with an argument like below. But I'm a php beginner and curious about the other methods.
function action($pet = "dog") {
$p = 'My'. $pet . 'likes to run.';
return $p;
}
$pet = 'cat';
echo action($pet);
Upvotes: 2
Views: 111
Reputation: 4484
Well, I know that you probably weren't looking for this, but it is late and I am bored.
Disclaimer: this is a bad idea.
class MyString
{
private $format, $pet;
public function __construct($format, &$pet)
{
$this->format = $format;
$this->pet = &$pet;
}
public function __toString()
{
return sprintf($this->format, $this->pet);
}
}
$myString = new MyString('This is my little %s, cool huh?', $pet);
$pet = 'cat';
echo $myString."\n";
$pet = 'dog';
echo $myString."\n";
$pet = 'goldfish';
echo $myString."\n";
Output:
This is my little cat, cool huh?
This is my little dog, cool huh?
This is my little goldfish, cool huh?
Demo: https://3v4l.org/XmUEZ
Basically this class stores a reference to the $pet
variable in it's fields. As such, when the $pet
variable is updated, the reference in the class is updated as well.
Another one for good measure:
function mysprintf($format, &$variable)
{
return function() use ($format, &$variable) {
return sprintf($format, $variable);
};
}
$print = mysprintf('This is my little %s, cool huh?', $pet);
$pet = 'cat';
echo $print()."\n";
$pet = 'dog';
echo $print()."\n";
$pet = 'goldfish';
echo $print()."\n";
(Ab)uses closures to save the reference. Probably even worse.
Why is this a bad idea, you ask?
Consider solely this statement:
$pet = 'goldfish';
This is a simple assignment. Most programmers assume that this statement has no side-effects. That means that this statement changes nothing in the execution flow besides creating a new variable.
Our MyString
or mysprintf
violate this assumption. What should be a simple assignment, now has side-effects. It violates a programmer's expectation in the worst way possible.
Upvotes: 3
Reputation: 38502
You are trying to create a Dynamic String Variable, which is not possible in php
for now. You can use the functional approach which you have implemented already :) .
function action($pet = "dog") {
$p = 'My'. $pet . 'likes to run.';
return $p;
}
$pet = 'cat';
echo action($pet);
if you use the below approach, unfortunately it will only covers printing part not when we need to reassign value of variable.
$pet ='dog';
$action=sprintf('My %s likes to run.', $pet);
$pet ='cat';
echo $action;
So, just stick with the function way :)
Upvotes: 2