任何人都可以看到我使用这个类做了什么incorreclty它应该改变之前和之后的单词,但它只是坐在那里什么都不做。 第二个php pgm是我正在使用的实际类..
<?php
include("parse.inc.php");
$bob = new parseClass();
$tag = "{Before}";
$replacement = "After";
$content = "My animal runs around all over the place and his name is {Before}.";
$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
?>
parse.inc.php (file name)
***************************
<?php
Class parseClass {
function ReplaceTag($tag, $replacement, $content) {
$content = str_replace($tag, $replacement, $content);
return $content;
}
}
// END Class parseClass
?>
答案 0 :(得分:1)
在您的情况下,您将内容传递给类的函数 因此,您应该将返回值存储到变量第一个
中$result = $bob->ReplaceTag($tag, $replacement, $content);
echo $result;
答案 1 :(得分:0)
您只需将函数的结果存储在局部变量中,然后打印出来。
$result = $bob->ReplaceTag($x, $y, $z);
print($result);
答案 2 :(得分:0)
我想你要打印要更换的字符串,所以你应该:
$result = $bob->ReplaceTag($tag, $replacement, $content);
echo $result;
为什么使用echo来打印类?如果你想这样做,请改用“var_dump”或“print_r”;
答案 3 :(得分:0)
如果硬性要求是您希望echo
parseClass
的实例,请尝试以下操作:
class parseClass
{
private $content;
public function ReplaceTag($tag, $replacement, $content)
{
$this->content = str_replace($tag, $replacement, $content);
}
public function __toString()
{
return $this->content;
}
}
$bob = new parseClass();
$tag = "{Before}";
$replacement = "After";
$content = "My animal runs around all over the place and his name is {Before}.";
$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
供参考,见:
答案 4 :(得分:0)
只需更换这两行
即可$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
与
$content = $bob->ReplaceTag($tag, $replacement, $content);
echo $content;