doudiyu1639 2015-09-05 12:26
浏览 73
已采纳

PHP扩展了类

Goodmorning everyone. I coded a class that return me:

Notice: Undefined variable: db in /Applications/MAMP/htdocs/Test Vari/index.php on line 12
Fatal error: Call to a member function row() on null in /Applications/MAMP/htdocs/Test Vari/index.php on line 12

This is the first part of the php file:

session_start();
$_SESSION['ID'] = 1;
require_once 'pass/password.inc.php'; /*Here's the DB Class*/

I extended a class into another, here's the code:

class pg extends DB{
   public $id;
   function __construct(){
      parent::__construct();
      $this->id = $_SESSION['ID'];
   }
   public function pgname(){
      $rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
      return($rs);
  }
}
 $pg = new pg(); 
 print_r ( $pg->pgname());

The $db->row() is declared in DB class that I extended, and I know for sure that's working. The DB class isn't initialized, and when I do, the error is the same, this is how I do it:

class pg extends DB{
    public $id;
    public $db;
    function __construct(){
        parent::__construct();
        $this->db = new DB();
        $this->id = $_SESSION['ID'];
    }
    public function pgname(){
        $rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
        return($rs);
    }
}

The Fatal Error will disappear when I delete braces in print_r($pg->pgname);

  • 写回答

2条回答 默认 最新

  • dongwen7813 2015-09-05 12:35
    关注

    The first way is just fine, but you need to remember that you are calling $db->row( as it exists in the parent class you need to use $this-> on it so

    class pg extends DB{
       public $id;
       function __construct(){
          parent::__construct();
          $this->id = $_SESSION['ID'];
       }
       public function pgname(){
          $rs = $this->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
          return($rs);
      }
    }
    
    $pg = new pg(); 
    print_r ( $pg->pgname());
    

    Also to keep the encapsulation of the class nice and tight, it would be better to pass the session to the constructor rather than get it from the $_SESSION inside the constructor like this :

    class pg extends DB{
       public $id;
       function __construct($id){
          parent::__construct();
          $this->id = $id;
       }
       public function pgname(){
          $rs = $this->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
          return($rs);
      }
    }
    
    $pg = new pg($_SESSION['ID']); 
    print_r ( $pg->pgname());
    

    I also assume you have included the file containing the DB class?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大