Tuesday, 15 March 2011

php - Initializing a class object inside another class -



php - Initializing a class object inside another class -

i have grouping class contains:

class groups { protected $_db; protected $member_id; protected $group_id,$group_title; public function __construct($db, $member_id) { $this->_db = $db; $this->member_id = $member_id; } /** * set grouping title * * @param str $group_id * */ public function set_group_id($group_id) { $this->group_id = $group_id; } /** * set grouping id * * @param int $group_id * */ public function get_group_id() { homecoming $this->group_id; } /** * set grouping title * * @param str $group_id * */ public function set_group_title($group_title) { $this->group_title = $group_title; } /** * grouping title * * @return int $group_id * */ public function get_group_title() { homecoming $this->group_title; } /** * build existing grouping id * * @param str $group_id * */ public function fetch_group_by_id($group_id) { $sql = "select * `gps` `id` = ".sql_c($group_id)." , (`temp` != 'on' or `temp` null) , (`backup` != 'on' or `backup` null)"; $result = @mysql_query($sql,$this->_db); check_sql(mysql_error(), $sql, 0); $list = mysql_fetch_array($result); $this->set_group_id($list['id']); //set grouping properties grouping $this->set_group_title($list['title']); } }

then created class called group_value

i want initialize grouping class within group_value class follows, when tried gives me unexpected error...

class group_value extends groups { protected $_db; protected $member_id; protected $comment, $member_id, $group_id, $match, $match_id; function __construct($db, $member_id) { $this->_db = $db; $this->member_id = $member_id; } $groups = new groups($db,$member_id); //here gives me error !! <<-- /** * set comment * * @param str $comment * */ public function set_comment($comment) { $this->comment = $comment; } }

to limit code repeating include groups class within group_value

move $groups = new groups($db,$member_id); constructor:

function __construct($db, $member_id) { parent::__construct($db, $member_id); $this->groups = new groups($db,$member_id); }

and define protected variable.

edit: changed phone call parent constructor.

php class oop

No comments:

Post a Comment