Access parent property from child
| From: | Suprie | Date: | Thu, 09 Aug 2007 10:12:30 +0000 |
| Subject: | Access parent property from child | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-260505@lists.php.net to get a copy of this message | ||
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
--
Jangan tanyakan apa yang Indonesia telah berikan pada mu
tapi bertanyalah apa yang telah engkau berikan kepada Indonesia
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GU/IT d- s: a-- C++ UL P L++ E W++ N* o-- K-
w PS Y-- PGP- t++ 5 X R++ tv b+ DI D+ G e+ h* r- z?
------END GEEK CODE BLOCK------