在课堂上找不到的方法

时间:2015-01-05 20:46:34

标签: php mysql phpstorm

我正在使用登录功能但遇到了一个我无法弄清楚的错误。

这是我的模型登录类:

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();
    }
}

路径是正确的,所以不是这样。可能是什么错误?

5 个答案:

答案 0 :(得分:14)

PhpStorm无法确定您的$this->cxn字段的类型。您可以通过简单的PHPDoc注释提供typehint来提供帮助:

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

答案 1 :(得分:10)

此提示也有助于PHPstorm正确识别方法

如果你有这样的代码:

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

只需添加此提示:

/* @var $entity \My\Name\Space\Path\MyEntity */
foreach( $myEntity as $entity ) {  
  $entity->myValidMethod();
}  

感谢http://mossco.co.uk/symfony-2/how-to-fix-method-not-found-in-class-orange-warning-in-phpstorm/

答案 2 :(得分: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

答案 3 :(得分:0)

您可以尝试function __destruct() {} Reference

而不是关闭()功能

问题在于缺少public / protected / private

答案 4 :(得分:0)

你必须添加" public"数据库类中的方法声明关键字恕我直言

public function close()