Php 构造函数中的调用方法
所以我的问题不是为什么,什么时候。因此,我们需要更改什么来更正代码Php 构造函数中的调用方法,php,class,constructor,this,fatal-error,Php,Class,Constructor,This,Fatal Error,所以我的问题不是为什么,什么时候。因此,我们需要更改什么来更正代码 class html { var $title; var $result; var $content; public function __construct(){ $title = "Untitled"; $content = "Content"; $this->setup_me(); } public functio
class html {
var $title;
var $result;
var $content;
public function __construct(){
$title = "Untitled";
$content = "Content";
$this->setup_me();
}
public function BLANK(){
$title = "Untitled";
$this->setup_me();
}
public function add($string){
$result = $string;
}
public function setup_me(){
$result = "$title--$content";
}
public function show(){
echo $result;
}
}
$new1 = new html();
$new2 = html::BLANK();
$new1->show();
$new2->show();
这是我的回报
Fatal error: Using $this when not in object context in /home/fcs.php on line 23
我在这里发现了一些问题,但没有人推荐一个切实可行的解决方案,到处都是解释,没有解决方案
请给我简单的更正,因为我认为我做得对。只是将$new2实例化为html对象
$new1 = new html();
$new2 = new html();
$new2->BLANK();
$new1->show();
$new2->show();
这里只是一个没有注释的工作版本;)
这很好,但是空构造函数返回与_构造函数相同的结果。在我的代码中,它返回_construct(Untitled--Content)BLANK(Untitled--)not WORK!这不是答案啊好吧,我知道你的问题,但是你不能有两个构造函数。始终只有一个,并且在创建对象的实例时调用它。因为在代码中,要在构造函数中设置title和content的默认值,必须在静态BLANK方法中将content的内容设置为空字符串,然后再次调用setup\u me。让我更新代码,试着调试一下。它甚至不返回字符。这不是我的问题的答案!也许你应该费心去阅读和理解这些解释。你的代码大多是胡说八道,不能工作。
class html
{
public $title;
public $result;
public $content;
public function __construct()
{
$this->title = "Untitled";
$this->content = "Content";
$this->setup_me();
}
public static function BLANK()
{
$html = new html();
$html->content = '';
$html->setup_me();
return $html;
}
public function add($string)
{
$this->result = $string;
}
public function setup_me()
{
$this->result = "{$this->title}--{$this->content}";
}
public function show()
{
return $this->result;
}
}
$new1 = new html();
$new2 = html::BLANK();
echo $new1->show()."\n";
echo $new2->show()."\n";