2016-06-24 12 views
2

私はPHP OOPの初心者です。私は、クラスを持っており、出力は空です: NAME1がある-missed POSTが<作業 作業:PHPクラスは何も返しません

$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; 
    } 
} 
?> 

、出力はユーザ名なしのテキストのみのですか?

+2

1)') 'あなただけの' getsomething(の戻り値を返すため)、かなり無用である 'とその機能がありませんNULLを返します。 – Rizier123

+0

@ Rizier123どのように私はちょうどこれが道だと言った。 –

答えて

2

でなければなりません_construct使用していた私は少しあなたのコードをmodifedとで遊ぶためにいくつかの例を追加しました。 これはあなたを開始するはずです。 `のgetName(2)リターンを強調2を必要と_construct`

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 
    } 
} 

$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(); 
2

こんにちはあなたはそれが__contrust(2 underscores)

関連する問題