Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Chaining... I just can't get it!
    primarykey
    data
    text
    <p>I'm trying to create a chaining function for working with strings that are returned from an XML file.</p> <p>1 original string may have multiple replacements, some of which come from the XML file.</p> <p>Here is the ugly and standard wrapped approach:</p> <p><code>str_replace("what","is meant", str_replace("name","randomer",str_replace("blah", "hello", $string1)));</code></p> <p>Here is the approach I'm trying to replicate (like Java):</p> <p><code>$string1.replace("blah","hello").replace("name","randomer").replace("what","is meant");</code></p> <p>With the above, it works easily... until I use the XML function to get the replacing string.</p> <p>Here's my class:</p> <pre><code>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 -&gt;replace("lpha","bravo") // abravo -&gt;replace("vo", resources::getString("charlie")-&gt;show()) // should be abracharlie -&gt;show(); // charlie </code></pre> <p>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.</p> <p>Thanks, Dominic</p> <p>EDIT: Yes!! I have figured it out (using statics) but it seems Ryano below has an even better solution</p> <pre><code>&lt;?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 -&gt;replace("lpha","bravo") // abravo -&gt;replace("vo", resources::getString("charlie")-&gt;show()) // should be abracharlie -&gt;replace("lie", resources::getString("vo")-&gt;show()) // abracharvo -&gt;show(); // abracharvo echo "&lt;br /&gt;"; echo resources::getInstance("randomer") // randomer -&gt;replace("er","") // random -&gt;replace("ran", resources::getString("")-&gt;show()) // dom -&gt;replace("dom", resources::getString("Dom")-&gt;show()) // Dom -&gt;show(); // Dom echo "&lt;br /&gt;"; echo resources::getInstance("nomster") // nomster -&gt;replace("nom","nmo") // nmoster -&gt;replace("nom", resources::getString("mon")-&gt;show()) // nmoster -&gt;replace("nmo", resources::getString("mon")-&gt;show()) // monster -&gt;show(); // monster ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload