1

我正在尝试创建一个链接函数来处理从 XML 文件返回的字符串。

1 个原始字符串可能有多个替换,其中一些来自 XML 文件。

这是丑陋和标准包装的方法:

str_replace("what","is meant", str_replace("name","randomer",str_replace("blah", "hello", $string1)));

这是我试图复制的方法(如 Java):

$string1.replace("blah","hello").replace("name","randomer").replace("what","is meant");

有了上面,它很容易工作......直到我使用 XML 函数来获取替换字符串。

这是我的课:

class resources{

private static $instance, $string;

public static function getString($stringName){
    # Create new instance
    self::$instance = new self;

    # Grabs stringName from an XML file
    self::$string = $stringName;

    # Return instance
    var_dump(self::$instance);
    return self::$instance;

}

public static function replace($replace_this, $with_this){
    # Replace and return instance
    self::$string = str_replace($replace_this, $with_this, self::$string);
    return self::$instance;
}

public static function show(){
    # Return String
    return self::$string;
}

}

echo resources::getString("alpha") // alpha
    ->replace("lpha","bravo") // abravo
    ->replace("vo", resources::getString("charlie")->show()) // should be abracharlie
 ->show(); // charlie

我希望它了解为什么它没有按我认为的那样工作以及它应该如何实际工作。似乎当我再次调用该类时(尽管 var_dump 说它是一个单独的实例),它用“charlie”替换了原始文本,所以我不能只替换第一位的一部分。

谢谢,多米尼克

编辑:是的!我已经想通了(使用静力学),但似乎下面的 Ryano 有一个更好的解决方案

<?php

class resources{
private static $instance, $string, $originalString;

public static function getInstance($stringName){
    self::$instance = new self();
    self::$originalString = $stringName;
    return self::$instance;
}

public static function getString($stringName){
    # Grabs stringName from an XML file
    self::$string = $stringName;
    return self::$instance;
}

function replace($replace_this, $with_this){
    self::$originalString = str_replace($replace_this, $with_this, self::$originalString);
    self::$string = self::$originalString;
    return self::$instance;
}

function show(){
    return self::$string;
}

}

echo resources::getInstance("alpha") // alpha
    ->replace("lpha","bravo") // abravo
    ->replace("vo", resources::getString("charlie")->show()) // should be abracharlie
    ->replace("lie", resources::getString("vo")->show()) // abracharvo
    ->show(); // abracharvo

echo "<br />";

echo resources::getInstance("randomer") // randomer
    ->replace("er","") //  random
    ->replace("ran", resources::getString("")->show()) // dom
    ->replace("dom", resources::getString("Dom")->show()) // Dom
    ->show(); // Dom

echo "<br />";

echo resources::getInstance("nomster") // nomster
    ->replace("nom","nmo") //  nmoster
    ->replace("nom", resources::getString("mon")->show()) // nmoster
    ->replace("nmo", resources::getString("mon")->show()) // monster
    ->show(); // monster

?>
4

2 回答 2

2

你的问题是一切都是静态的。我建议复习一些面向对象的编程基础知识。

因为一切都是静态的,所以状态在函数的所有调用之间共享。在该行replace("vo", resources::getString("charlie")->show())中,对 的嵌套调用resources::getString将到目前为止构建的字符串 ( abravo) 替换为getStringis的参数charlie。然后包装函数被调用 like replace("vo", "charlie"),但是 的值self::$string是 now charlie,它不包含vo,因此 finalshow()然后简单地返回charlie。如果您使用 而不是vo调用它replace("ar", resources::getString("charlie")->show()),则 finalshow()将改为返回chcharlielie

您必须创建一个具有非静态成员变量和方法的类,以便维护单独的状态。

这是一个工作版本:

class resources {

  private $string;

  public function __construct ($string) {
    $this->string = $string;
  }

  public static function getString ($string) {
    $obj = new resources($string);

    return $obj;
  }

  public function replace ($replace_this, $with_this) {
    # Replace and return instance
    $this->string = str_replace($replace_this, $with_this, $this->string);
    return $this;
  }

  public function show () {
    # Return String
    return $this->string;
  }

}

编辑:我喜欢上面的代码作为与问题代码最接近的过渡。如果我自己写类似的东西,我会像这样进一步简化它:

class Str {
    private $str;

    private function __construct ($str) {
      $this->str = $str;
    }

    public static function with ($str) {
        return new Str($str);
    }

    public function replace($replace_this, $with_this) {
      $this->str = str_replace($replace_this, $with_this, $this->str);
      return $this;
    }

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

echo Str::with('nomster')->replace('nom', 'mon') . "\n";

现在不需要了show(),而且名称更易于键入。这里可以添加许多其他有用的方法;您想要链接的任何 php 字符串函数。

于 2011-06-13T22:23:49.550 回答
0

当您getString()多次调用时,您会创建几个实例,因为您调用new self().getString()

为了防止这种情况发生,您应该创建一个方法getInstance()并在getString()

public static function getInstance() {
    if(!self::instance) {
        self::instance = new self();
    }
    return self::instance;
}

public static function getString() {
    $instance = self::getInstance();

    // use $instance here instead of self::instance
}
于 2011-06-13T21:10:52.310 回答