PHP类不返回任何内容
我只是PHP OOP的初学者。我有一个类,输出为空:PHP类不返回任何内容,php,class,oop,Php,Class,Oop,我只是PHP OOP的初学者。我有一个类,输出为空: $test = new form('name1', 'passw2'); $test->getName(); 和类别: <?php class form { protected $username; protected $password; protected $errors = array(); function _construct($username, $password
$test = new form('name1', 'passw2');
$test->getName();
和类别:
<?php
class form
{
protected $username;
protected $password;
protected $errors = array();
function _construct($username, $password){
$this->username=$username;
$this->password=$password;
}
public function getsomething() {
echo '<br>working'. $this->getn() . '<-missed';
}
public function getName(){
return $this->getsomething();
}
public function getn() {
return $this->username;
}
}
?>
并且输出仅为不带用户名的文本:
后期工作
工作您好,您已经使用了
\u construct
它应该是\u contrust(2个下划线)
我对您的代码做了一些修改,并添加了一些示例。
这应该让你开始
class form
{
protected $username;
protected $password;
protected $errors = array();
// construct is a magic function, two underscores are needed here
function __construct($username, $password){
$this->username = $username;
$this->password = $password;
}
// functions starting with get are called Getters
// they are accessor functions for the class property of the same name
public function getPassword(){
return $this->password;
}
public function getUserName() {
return $this->username;
}
public function render() {
echo '<br>working:';
echo '<br>Name: ' . $this->username; // using properties directly
echo '<br>Password:' . $this->password; // not the getters
}
}
1)
\u构造
需要2个下划线2)在getName()
中返回非常无用,因为您只返回getsomething()
的返回值,而该函数只返回NULL。@Rizier123我刚才说的这是一条线索。
$test = new form('name1', 'passw2');
// output via property access
echo $test->username;
echo $test->password;
// output via getter methods
echo $test->getUserName();
echo $test->getPassword();
// output via the render function of the class
$test->render();