Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php错误字符串转换_Php_Class - Fatal编程技术网

Php错误字符串转换

Php错误字符串转换,php,class,Php,Class,我有这个密码。我需要打印这些字符串。但是我不能在课外做改变。所以我需要在班里换工作。字段必须保持私有。有什么想法吗 class STUDENT { private $nume,$prenume; # Constructor public function __construct($nume , $prenume){ $this->nume=$nume; $this->prenume=$prenume;

我有这个密码。我需要打印这些字符串。但是我不能在课外做改变。所以我需要在班里换工作。字段必须保持私有。有什么想法吗

class STUDENT {
    private $nume,$prenume;
    # Constructor 
    public function __construct($nume , $prenume){ 
        $this->nume=$nume;
        $this->prenume=$prenume;            
    } 

}

$student = new STUDENT("one","two");  
echo "student: ". $student ."<hr/>";  
班级学生{
私人$nume,$prenume;
#建造师
公共函数构造($nume,$prenume){
$this->nume=$nume;
$this->prenume=$prenume;
} 
}
$student=新学生(“一名”、“两名”);
回声“学生:”$学生“
”;
您需要使用getter和setter,以便可以读取和写入字段。这里有一个关于这个主题的讨论:

您必须定义方法。然后,您可以将
echo
实例作为
string

class STUDENT {
    private $nume,$prenume;

    public function __construct($nume , $prenume){ 
        $this->nume=$nume;
        $this->prenume=$prenume;            
    } 

    public function __toString()
    {
        return '{nume:'.$this->nume.','.prenume:'.$this->prenume.'}';
    }
}

$student = new STUDENT("one","two");  
echo "student: ". $student ."<hr/>";  
班级学生{
私人$nume,$prenume;
公共函数构造($nume,$prenume){
$this->nume=$nume;
$this->prenume=$prenume;
} 
公共函数
{
返回“{nume:”.$this->nume.”,“.prenume:”.$this->prenume.}”;
}
}
$student=新学生(“一名”、“两名”);
回声“学生:”$学生“
”;
您必须实现
\uuu toString
方法:谢谢。它解决了这个问题。现在您需要正确响应,以便我可以添加reputation.public function\uuu toString(){return$this->nume.““$this->prenume;}这是错误的吗?嗯。显然,学生姓名需要是红色的。你能帮忙吗?谢谢。我用在其他问题上。我有一些例子。。我需要学习二传手,盖特和魔术。