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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?