0

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?

1
  • I assume you cannot. Commented May 21, 2017 at 10:08

3 Answers 3

2

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.

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

Comments

1

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

Comments

0

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

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

Comments

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.