PHP 链接...我就是不明白!

发布于 2024-11-15 00:56:28 字数 2985 浏览 2 评论 0原文

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

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

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

str_replace("what","ismean", str_replace("name","randomer",str_replace("blah", "hello", $string1)));< /code>

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

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

通过上面的代码,它可以工作很容易...直到我使用 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

?>

I'm trying to create a chaining function for working with strings that are returned from an XML file.

1 original string may have multiple replacements, some of which come from the XML file.

Here is the ugly and standard wrapped approach:

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

Here is the approach I'm trying to replicate (like Java):

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

With the above, it works easily... until I use the XML function to get the replacing string.

Here's my class:

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

I'd like it to understand why it's not working as I think it should and how it should actually work.
It seems that when I call the class again (despite var_dump saying its a seperate instance), it replaces the original text with "charlie" so I can't just replace a part of the first bit.

Thanks, Dominic

EDIT: Yes!! I have figured it out (using statics) but it seems Ryano below has an even better solution

<?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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-11-22 00:56:28

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

因为一切都是静态的,所以状态在函数的所有调用之间共享。在 replace("vo", resources::getString("charlie")->show()) 行中,对 resources::getString 的嵌套调用替换了到目前为止构建的字符串 (abravo),其参数为 getString,即 charlie。然后调用包装函数,如 replace("vo", "charlie"),但 self::$string 的值现在是 charlie,它不包含 vo,因此最终的show() 仅返回 charlie。如果您使用 replace("ar", resources::getString("charlie")->show()) 来调用它,而不是 vo,则最终的show() 将返回 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 字符串函数。

Your problem is that everything is static. I would suggest brushing up on some object-oriented programming fundamentals.

Because everything is static, the state is shared between all invocations of the functions. In the line replace("vo", resources::getString("charlie")->show()), the nested call to resources::getString replaces the string built so far (abravo) with the argument to getString which is charlie. Then the wrapping function is called like replace("vo", "charlie"), but the value of self::$string is now charlie, which does not contain vo and therefore the final show() then returns simply charlie. If, instead of vo, you'd called it with replace("ar", resources::getString("charlie")->show()), the final show() would have instead returned chcharlielie.

You must create a class with non-static member variables and methods in order to maintain separate states.

Here's a working version:

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;
  }

}

Edit: I like the above code as the closest transition from the question's code. If I was writing something similar myself, I would simplify it further like this:

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";

Now there's no need for show() and the names are a little nicer to type. Many other useful methods could be added here; any php string function you would want to chain.

一紙繁鸢 2024-11-22 00:56:28

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

为了防止这种情况发生,您应该创建一个方法 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
}

When you call getString() several times, you create several instances since you call new self() in getString().

To prevent that from happening you should create a method getInstance() and use that in 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文