PHP 链接...我就是不明白!
我正在尝试创建一个链接函数来处理从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是一切都是静态的。我建议温习一些面向对象的编程基础知识。
因为一切都是静态的,所以状态在函数的所有调用之间共享。在show() 仅返回
replace("vo", resources::getString("charlie")->show())
行中,对resources::getString
的嵌套调用替换了到目前为止构建的字符串 (abravo
),其参数为getString
,即charlie
。然后调用包装函数,如replace("vo", "charlie")
,但self::$string
的值现在是charlie
,它不包含vo
,因此最终的charlie
。如果您使用replace("ar", resources::getString("charlie")->show()) 来调用它,而不是show()
将返回vo
,则最终的chcharlielie
。您必须创建一个具有非静态成员变量和方法的类才能维护单独的状态。
这是一个工作版本:
编辑:我喜欢上面的代码作为与问题代码最接近的转换。如果我自己写类似的东西,我会像这样进一步简化它:
现在不需要
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 toresources::getString
replaces the string built so far (abravo
) with the argument togetString
which ischarlie
. Then the wrapping function is called likereplace("vo", "charlie")
, but the value ofself::$string
is nowcharlie
, which does not containvo
and therefore the finalshow()
then returns simplycharlie
. If, instead ofvo
, you'd called it withreplace("ar", resources::getString("charlie")->show())
, the finalshow()
would have instead returnedchcharlielie
.You must create a class with non-static member variables and methods in order to maintain separate states.
Here's a working version:
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:
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.当您多次调用
getString()
时,会创建多个实例,因为您在getString()
中调用了new self()
。为了防止这种情况发生,您应该创建一个方法
getInstance()
并在getString()
中使用它When you call
getString()
several times, you create several instances since you callnew self()
ingetString()
.To prevent that from happening you should create a method
getInstance()
and use that ingetString()