RE: [PHP] Access parent property from child
| From: | Edward Kay | Date: | Thu, 09 Aug 2007 11:29:59 +0000 |
| Subject: | RE: [PHP] Access parent property from child | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260506@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Suprie [mailto:suprie1983@gmail.com]
> Sent: 09 August 2007 11:13
> To: php-general@lists.php.net
> Subject: [PHP] Access parent property from child
>
>
> dear all ...
>
> i have question, i've tried look at google but still can't figured out
> how to do it...
> i want access parent property from child object the code was like
>
> <?
>
> class DAO
> {
> private $db;
> private $id;
>
> protected function getId()
> {
> return $this->id;
> }
> protected function setId($id)
> {
> $this->id = $id;
> }
>
> function getDB()
> {
> return $this->$db;
> }
> function setDB($db)
> {
> $this->db = $db;
> }
>
> public function load($id)
> {
> $query = "SELECT * FROM ".
> constant(get_class($this)."::tableName").
> " WHERE ".
> constant(get_class($this)."::pkFields").
> "='".$id."'";
> $rs = $this->db->Execute($query);
> $cObj = $rs->FetchObject(true);
>
> return $cObj;
> }
>
> public function save($rowData)
> {
> $this->db->debug = true;
> $query ="Select * FROM
> ".constant(get_class($this)."::tableName")."\n";
> $res =$this->db->Execute($query);
> $insertSQL = $this->db->GetInsertSQL($res,$rowData);
> $res =$this->db->Execute($insertSQL);
> if($res)
> return true;
>
> return false;
> }
>
> public function update($row, $id)
> {
> $query = "SELECT * FROM
> ".constant(get_class($this)."::tableName")."\n"
> . "WHERE
>
> ".constant(get_class($this)."::pkFields")."='".$id."'";
> $res = $this->db->Execute($query);
> $updateSQL = $this->db->GetUpdateSQL($res,$row);
> $db->Execute($updateSQL);
> }
>
> public function getAll($criteria='', $order='')
> {
> if(empty($order))
> $order = constant(get_class($this)."::pkFields");
>
> $query = "SELECT * FROM
> ".constant(get_class($this)."::tableName")."\n"
> . $criteria
> . "ORDER BY ".$order;
> $row = $this->db->Execute($query);
> if(!$row)
> {
> $this->error = $this->db->ErrorMsg();
> return false;
> }
> return $row->getArray();
> }
>
> }
>
> and i have this child
> <?php
>
> include 'BaseDAO.class.php';
> class TaskCLDAO extends DAO
> {
> const tableName = 'task';
> const pkFields = 'task_id';
>
> function findWithRelation($id)
> {
> $query = "select * from task LEFT JOIN log ON
> task.id=log.task_id";
> $db = parent::getDB();
> $res = $db->Execute($query);
> return $res->getArray();
> }
> }
>
>
> ?>
>
> and this is the class that called it
>
> <?php
>
> $obj = new TaskCLDAO();
> $obj->setDB($db); // i have another file called config that
> initialized the db;
> $res = $obj->findWithRelation(1);
>
> ?>
>
> but the php's said
>
> Fatal error: Cannot access empty property...
>
> what does it mean ?? is there another way to access "$db" that
> parent have ?
>
> TIA
In function getDb()
return $this->$db;
should be:
return $this->db;
Also, although not strictly required, I suggest you add the public keyword
to your methods that don't specifiy their visibility.
Edward