【问题标题】:Method not found in class类中找不到方法
【发布时间】:2016-02-24 22:04:44
【问题描述】:

我正在处理登录功能,但出现了一个我无法弄清楚的错误。

这是我的模型登录类:

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

最后一个函数给出错误“在类中找不到方法'close'”,“在主题类中找不到引用的方法。

这是我的数据库类:

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

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

【问题讨论】:

  • 请停止使用过时的mysql_* 函数,并学习如何将 PDO 与准备好的语句一起使用。
  • 我很难看到真正的问题。你能具体说明你是如何使用这些对象的吗?

标签: php mysql phpstorm


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这个提示也有助于 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/

    【讨论】:

      【解决方案3】:

      我认为代码中没有可见性问题。 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

      【讨论】:

        【解决方案4】:

        您可以尝试使用 close() 函数而不是 function __destruct() {} Reference

        问题在于缺少公共/受保护/私有

        【讨论】:

        • 这里唯一的问题是php中类中函数的默认可见性是public。
        【解决方案5】:

        您必须在数据库类恕我直言的方法声明中添加“public”关键字

        public function close()
        

        【讨论】:

          猜你喜欢
          • 2014-03-24
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多