user3574603
user3574603

Reputation: 3618

PHP: How do I create an interpolated string that changes when inner variables change?

I want to store a sentence in a variable. The sentences contains another variable.

php > $name = "Fred";
php > $sentence = "Your name is {$name}";
php > echo $sentence;
Your name is Fred

If I change the value of $name, the sentence is unchanged:

php > $name = "John";
php > echo $sentence;
Your name is Fred

But I want the sentence to become 'Your name is John'. Is there a way in PHP I can create an iterpolated string that changes when the inner string changes?

Upvotes: 0

Views: 67

Answers (3)

timiTao
timiTao

Reputation: 1413

No, it not possible the way you want it to working.

Other solutions:

function

function mySentence($name) {
  return 'Your name is '. $name;
}

Any other replace string

  • sprintf('"Your name is %s', $name);
  • str_replace('{name}', $name, 'Your name is {name}');

Sentence as object

Create class that holds main sentance as ValueObject

class Sentence {
    private $sentence;

    private $name;

    public function __constructor($name, $sentence){
        $this->name = $name;
        $this->sentence = $sentence;
    }

    public function changeName($name){
        return new Sentence($name, $this->sentence);
    }

    public function printText() {
        return $this->sentence . $this->name;
    }

    public function __toString(){
        return $this->printText();
    }
}

Then use simply:

$sentence = new Sentence('Fred', "My name is");
echo $sentence;
// My name is Fred

echo  $sentence->changeName('John');
// My name is John

This is of course idea, what options you have to resolve this. With that, you can add any replaceable placeholders etc.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Expanding on the "Sentence as object" portion of timiTao's answer, here's a more general-purpose "Template string" class you can use as a starting point for things like this.

class TemplateString {
    // Usage:
    // $t = new TemplateString("Your name is {{name}}");
    // $t->name = "John";
    // echo $t; // Your name is John

    private $template;
    private $parameters = [];
    public function __construct(string $template, array $defaultparams = null) {
        $this->template = $template;
        if( $defaultparams) $this->parameters = $defaultparams;
    }
    public function __set($k,$v) {
        $this->parameters[$k] = $v;
    }
    public function __get($k) {
        if( array_key_exists($k,$this->parameters)) {
            return $this->parameters[$k];
        }
        return null;
    }
    public function __toString() {
        $words = $this->parameters;
        return preg_replace_callback("/\{\{(\w+)\}\}/",function($m) use ($words) {
            if( array_key_exists($m[1],$words)) return $words[$m[1]];
            return $m[0];
        },$this->template);
    }
}

Try it online

Upvotes: 1

Gerriet
Gerriet

Reputation: 1350

What you want to do is not possible in php. Create a function returning the string.

func getSentence($name) {
  return "Your name is $name";
}

Upvotes: 0

Related Questions