繁体   English   中英

理解如何在php中引用父类的问题

[英]Problems understanding how to reference a parent class in php

OOP的新手,如果很明显,请抱歉。

我有一个班和一个儿童班

class dBConnect {

private $host, $username, $password, $database, $input;

public function __construct($host, $username, $password, $database){
    $this->host        = $host;
    $this->username    = $username;
    $this->password    = $password;
    $this->database    = $database;


    $this->dbConnect = new mysqli( $this->host, $this->username, $this->password, $this->database);
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
        }
    return true;
}

public function setCharacterSet($charSet = 'utf8'){
   if(!$this->dbConnect->set_charset($charSet)){
    printf("Error loading character set ".$charSet.": %s\n", $this->dbConnect->error);   
    };
    return true;
    }


   public function sanitizeSQL($input){
   return(mysqli_real_escape_string($this->dbConnect, $input));
   }

   public function runQuery($query) {
     $result = $this->dbConnect->query($query);
       if (!$result) die('Invalid query: ' . mysql_error($this->dbConnect));
      return $result;
      }

public function __destruct() {
    mysqli_close($this->dbConnect)
        OR die("There was a problem disconnecting from the database.");

   }

 }

 class newUser extends dBConnect {

    public static function login($userName=NULL, $password=NULL) {

       $sql = "SELECT * FROM db26541116_farmFile.users WHERE userName = '".$userName."' AND password = '".$password."' LIMIT 0,1";
       $result=parent::runQuery($sql);
      $numRows = $result->num_rows;
    $result->close();
    if($numRows > 0){
      return false;
    }else{
    $sql = "INSERT INTO db26541116_farmFile.users (userName, password) VALUES  ('$userName', '$password')"; 
    $result=parent::runQuery($sql);
    $result->close();
    return true;    
    }

    throw new RuntimeException("NULL string found in cleanPostString");

}

}

我可以创建对象

      $db = new dBConnect("localhost", "userName", "password", "database");
      $db->setCharacterSet('utf8'); 

没有问题,但是当我使用子类时,我有问题

    if(newUser::login($userName,$passWord)){
//send to site
}else{
//try again 
}

我收到错误消息非静态方法dBConnect :: runQuery()不应被静态调用。 我应该如何从newUser中调用父类。

谢谢您的期待。

$result=parent::runQuery($sql);

错误在这一行。 您已将runQuery定义为公共函数,因此应使用$this->调用。 如果已将其定义为public static,则可以像parent::那样调用它。

在此行将发生相同的错误:

$result=parent::runQuery($sql);

您不应一次调用非静态方法。 相反的说法不正确-您可以在非静态方法中调用静态方法。 您应该阅读php文档以获取更多信息。

您应该实例化newUser类而不是父类。 并使用其对象登录用户。

$newUser = new newUser("localhost", "farmFile01", "NheuYyjadk3hdCga", "db26541116_farmFile"); // will use the construct of the parent class
$newUser->setCharacterSet('utf8'); 
if(newUser->login($userName,$passWord)){
//send to site
}else{
//try again 
}

也许更好,不要扩展数据库类,而是像这样传递数据库对象。

$newUser = new newUser($db);

与结合

class newUser extends dBConnect {
  private $db;
  public function __construct($db){
  $this->db = $db;
  }
}
// and to run a query use 
$result=$this->db->runQuery($sql); 

您可以这样更改代码:

if(newUser::login($userName,$passWord,$db)){
//send to site
}else{
//try again 
}

在newUser类中,您需要像这样进行更改

public static function login($userName=NULL, $password=NULL,newUser $newUser) {
 $result=$newUser->runQuery($sql);
}

然后您的代码将正常运行。

除了代码中解决以下问题的一些错误之外,下面还应概述该问题并为您提供一个解决之道。

调用创建的方法的方式实际上是这样的:

//No instantiation needed ie $newUser = new newUser()
//It is static so you can just call it.
newUser::login();

但是,您会收到错误消息Non-static method dBConnect::runQuery() should not be called statically

这是因为要调用dbConnectrunQuery($sql) ,必须实例化该对象的实例。 newUser扩展,因此从技术上讲,这是您需要创建的内容。

$newUser = new newUser('host','user','pass','db'); //Notice this is dbConnects constructor.
$newUser->login();

但是,您将再次收到错误消息: Non-static method dBConnect::runQuery() should not be called statically调用,因为您仍在静态方法中调用它。

因此,解决方案非常简单, static从方法login删除static标识符即可。 从技术上讲,您现在可以parent::runQuery($sql);会它,但是您可能想要更新parent::runQuery($sql); $this->runQuery($sql); 因为调用parent将获得dbConnectrunQuery($sql)并说有一天您需要在newUser覆盖它, newUser您将newUser一个错误并且不知道为什么。

注意, $this是对象的此实例的缩写,不适用于静态方法。 静态方法不是实例的一部分,而是对象本身。 由于您不必实例化对象即可使用该方法,因此没有此实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM