2016-06-24 3 views
2

저는 PHP OOP로 초보자입니다. 나는 클래스가 출력이 비어 : 이름 1 인 -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)' 그냥 NULL을 반환합니다. – Rizier123

+0

@ Rizier123 방금 내가 흔적이라고 말한 방법. –

답변

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 getName`2) 수익을()`방금 getsomething (`의 반환 값을 반환 이후), 꽤 쓸모가`그 함수가 강조 필요 _construct`
2

안녕 당신은 내가 당신의 코드를 조금 변형 한 몇 가지 예에 놀러 추가 한 __contrust(2 underscores)

관련 문제