Php 在类中设置变量
我想做这样的事情:Php 在类中设置变量,php,oop,constructor,Php,Oop,Constructor,我想做这样的事情: <?php $editor = new editor('reply.php?topic=100', 'simple'); echo $editor; ?> 但我不熟悉OOP/类,但我想在课堂上做一些类似的事情: <?php class editor($url, $type) { if($type == 'simple'){ ?> <form action="<?php e
<?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=neweditor('reply.php?topic=100','simple');
echo$编辑器;
类编辑器
{
私有$url;
私人$type;
公共函数构造($url,$type)
{
$this->url=$url;
$this->type=$type;
}
公共函数
{
如果($this->type=='simple'){
返回“”;
}否则{
返回“foobar”;
}
}
}
这是非常基本和必要的内容,所以如果您熟悉PHP的OO语法,这是非常有意义的。我相信,几乎每个人都能给你一个固定的答案,但还是帮自己一个忙,试着自己回答这个简单的问题。以下是官方文件:
你的问题的直接答案是神奇的方法,但你到底想做什么?您确定类是您试图解决的问题的解决方案吗?为什么不能只使用返回HTML代码的函数?@mc10:PEAR使用这个大括号样式,所以没有什么不一致:)@BoltClock:你在和你想象中的朋友说话吗?;-)
class MyClass
{
private $_var;
public function __constructor($var)
{
$this->_var = $var;
}
public function action()
{
echo $this->_var;
}
}
$obj = new MyClass('abcd');
$obj->action();
$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';
}
}
}