我想做这样的事情:
<?php
$editor = new editor('reply.php?topic=100', 'simple');
echo $editor;
?>
但是我不熟悉 OOP/类,但我想在类中做这样的事情:
<?php
class editor($url, $type)
{
if($type == 'simple'){
?>
<form action="<?php echo $url; ?>">
...
</form>
<?php
}
else
{
...
}
}
?>
当然,这是不对的。但我怎样才能做到呢?因为我对构造函数和析构函数等一无所知......
最佳答案
$editor = new editor('reply.php?topic=100', 'simple');
echo $editor;
class editor
{
private $url;
private $type;
public function __construct($url, $type)
{
$this->url = $url;
$this->type = $type;
}
public function __toString()
{
if($this->type == 'simple'){
return '<form action="' . $this->url . '"></form>';
} else {
return 'foobar';
}
}
}
关于php - 在类中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5448928/