c_shelswell Posted April 9, 2009 Share Posted April 9, 2009 Hi still learning classes and I think there's a better way to do what i'm doing. I have a database class which logs on to db and does any queries I need. I'm also making a form class in a separate file when a user submits a registration it checks if the username is taken for this it needs the database class. I'm calling it like this: <?php class form { public function checkFields($postData) { /** check username available **/ $db = new db(); $data = array('fields'=>'username', 'table'=>'registered_users', 'where'=>'username="'.$postData['username'].'"'); $rtn = $db->selectFromDB($data); if ($rtn[0]->username > '') { $this->setError('username', '<br />This user name is already in use. Please choose another.'); } } ?> but can't I have something like <?php class form extends db{ public function checkFields($postData) { /** check username available **/ $data = array('fields'=>'username', 'table'=>'registered_users', 'where'=>'username="'.$postData['username'].'"'); $rtn = db::selectFromDB($data); } } ?> if i do it the 2nd way it seems to call the db class as I get a mysql access denied error so i guess it's not calling the database constructor? I don't think i'm fully grasping the extends concept I have been reading up on it but some help would be great. The start of my database class is below cheers: <?php class db { private $dbUser; private $dbName; private $dbPass; private $dbServer; private $connection; private $result; public function __construct($dbUser=DB_USER, $dbName=DB_NAME, $dbPass=DB_PASS, $dbServer=DB_SERVER) { $this->dbUser = $dbUser; $this->dbName = $dbName; $this->dbPass = $dbPass; $this->dbServer = $dbServer; $this->connect(); } private function connect() { $this->connection = mysql_pconnect($this->dbServer, $this->dbUser, $this->dbPass) or die (mysql_error()); mysql_select_db($this->dbName, $this->connection)or die(mysql_error()); } private function disconnect() { mysql_close($this->connection); } public function selectFromDB($data) { extract($data); $sql = 'SELECT '.$fields.' FROM '.$table; if(!empty($where)) { $sql .= ' WHERE '.$where; } if(!empty($groupBy)) { $sql .= ' GROUP BY '.$groupBy; } if(!empty($orderBy)) { $sql .= ' ORDER BY '.$orderBy; } if(!empty($limit)) { $sql .= ' LIMIT '.$limit; } $this->result = $this->query($sql); if($this->result) { $rows = $this->fetchRows(); } return $rows; } } ?> Link to comment https://forums.phpfreaks.com/topic/153320-solved-help-extending-a-class/ Share on other sites More sharing options...
Mark Baker Posted April 9, 2009 Share Posted April 9, 2009 In your form class constructor, you need to have a call to parent::_construct() Link to comment https://forums.phpfreaks.com/topic/153320-solved-help-extending-a-class/#findComment-805493 Share on other sites More sharing options...
c_shelswell Posted April 9, 2009 Author Share Posted April 9, 2009 Great thanks very much - works a treat! Link to comment https://forums.phpfreaks.com/topic/153320-solved-help-extending-a-class/#findComment-805504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.