2015-01-05 4 views
2

로그인 기능을 사용하고 있지만 알아낼 수없는 오류가 있습니다.클래스에 메소드가 없습니다.

이 내 모델 로그인 클래스입니다 :

class Login { 

    private $username; 
    private $password; 
    private $cxn;  //database object 

    function __construct($username,$password) 
    { 
     //set data 
     $this->setData($username, $password); 
     //connect DB 
     $this->connectToDB(); 
     // get Data 
    } 

    function setData($username, $password) 
    { 
     $this->username = $username; 
     $this->password = $password; 
    } 

    private function connectToDB() 
    { 
     include 'Database.php'; 
     $connect = '../include/connect.php'; 
     $this->cxn = new database($connect); 
    } 

    function getData() 
    { 
     $query = "SELECT * FROM anvandare WHERE anvandarnamn = '$this->username' AND losenord ='$this->'password'"; 
     $sql = mysql_query($query); 

     if(mysql_num_rows($sql)>0) 
     { 
      return true; 
     } 
     else 
     { 
      throw new Exception("Användarnamnet eller Lösenordet är fel. Försök igen."); 
     } 
    } 

    function close() 
    { 
     $this->cxn->close(); //Here is the error it says "Method 'close' not found in class 
    } 
} 

마지막 기능은 "참조 된 방법은 대상 클래스에없는."

, 오류 " '가까운'클래스에서 찾을 수 없습니다 방법"을 제공합니다 여기

내 데이터베이스 클래스입니다 :

class Database { 

    private $host; 
    private $user; 
    private $password;     
    private $database; 

    function __construct($filename) 
    {  
     if(is_file($filename)) include $filename; 
     else throw new exception("Error!"); 

     $this->host=$host; 
     $this->user=$user; 
     $this->password=$password; 
     $this->database=$database; 

     $this->connect(); 
    } 

    private function connect() 
    { 
     //Connect to the server 
     if(!mysql_connect($this->host, $this->user, $this->password)) 
     throw new exception("Error, not connected to the server."); 

     //Select the database 
     if(!mysql_select_db($this->database)) 
     throw new exception("Error, no database selected");    
    } 

    function close() 
    { 
     mysql_close(); 
    } 
} 

경로는 올바른들입니다 그것은 그렇지 않습니다. 어떤 오류가있을 수 있습니까? 대신 close()를 기능의

+2

오래된 mysql_ * 함수를 사용하지 말고 준비된 구문으로 PDO를 사용하는 법을 배우십시오. –

+0

실제 문제를보기가 어렵습니다. 이 객체를 정확히 사용하는 방법을 지정할 수 있습니까? – stefan

답변

12

PhpStorm에서 $this->cxn 필드의 유형을 알 수 없습니다.

foreach($myEntity as $entity) { 
    $entity->myValidMethod(); 
} 

그냥이 추가

/** @var Database */ 
private $cxn;  //database object 
+1

감사합니다! 사랑해 –

0

당신이 function __destruct() {}Reference

문제를 시도 할 수는없는에서 공개/보호/개인

당신은 당신의 데이터베이스 클래스에 메서드 선언에 "공개"키워드를 추가해야합니다
+0

여기서 유일한 문제는 php의 클래스에있는 함수의 기본 공개 설정이 public이라는 것입니다. –

0

IMHO

public function close() 
1

코드에 가시성 문제가 있다고 생각하지 않습니다. PHP 매뉴얼의 내용 :

Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public. 

두 개의 데이터베이스 클래스가 있다고 생각합니다. 유사한 주제가이 주제에서 설명되었습니다. best way to call function from an extended class

관련 문제