开发者

using a variable to referance another in a class in PHP

开发者 https://www.devze.com 2023-01-14 15:40 出处:网络
so here\'s a cut down version of my code: class lookAClass { public $pageTitle; public function add() {

so here's a cut down version of my code:

class lookAClass
{
 public $pageTitle;

 public function add()
 {
  $tmp = 'just some filler text <[pageTitle]> and so开发者_如何学JAVAme more text';
  echo preg_replace_callback('<\<\[(.*)\]\>>', array(&$this, 'parseAdd'), $tmp);
 }

 private function parseAdd($matches)
 {
  return $this->$matches[1];
 }


}

$main = new lookAClass();

$main->add();

So basically what I'm trying to do is replace "<[pageTitle]>" with $this->pageTitle

I know $this->$matches[1] is incorrect but I can't seem to find how to make the connection.


How about this:

echo str_replace('<[pageTitle]>', $this->pageTitle, $tmp);
0

精彩评论

暂无评论...
验证码 换一张
取 消