class session + heritage
Posté : 12 déc. 2005, 17:34
Yop tout le monde,
voila je galere un peu. Je voulais faire une class session heritant des propriétes et méthode de ma class Mysql, mais j'ai une erreur
Ce qui veut dire je suppose qu'il ne s'attend pas a avoir une méthode d'objet a cette endroit.
Quelqu'un a une idée ?
[edit] Sur la construction de l'objet je me suis inspiré de cet article
ps: ma classe mysql marche nikel
jvous la met la pour ceux qui voudrait y jeter un coup d'oeil

voila je galere un peu. Je voulais faire une class session heritant des propriétes et méthode de ma class Mysql, mais j'ai une erreur
Code : Tout sélectionner
parse error, unexpected T_OBJECT_OPERATOR line 15<?php
/* Sessions inherit all the Mysql's class methods */
require("class.mysql.php");
class Sessions extends Mysql {
public $loginStatus;
private $sessionId;
//__construct create a new Sessions object
//connecting to the mysqldatabase
function __construct(){
parent::__construct();
this->connect(); //line 15
}
...
}
?>
Mais bon je comprend pas trop. J'appel juste le meme constructeur que pour ma classe mysql et apres j'utilise juste ma methode connect() et c'est apparement la dessus qu'il tilte Quelqu'un a une idée ?
[edit] Sur la construction de l'objet je me suis inspiré de cet article
ps: ma classe mysql marche nikel
jvous la met la pour ceux qui voudrait y jeter un coup d'oeil
<?php
class Mysql {
/*
* $error: error report
*/
public $error;
/*
* $host: the name or IP address of the MySQL server
*/
private $host;
/*
* $login: login and your password to access to the MySQL server
*
*/
private $login;
private $password;
/*
* $DB: name of the database you need to connect to
*/
private $DB;
/*
* $connect_id: connection identifiant
*/
private $connect_id;
/*
* $result_id: query result identifiant
*/
private $result_id;
/*
* __construct create a new MySQL object
* eg: $DB = new Mysql();
*/
function __construct($aHost='xxx',$aLogin='xxx',$aPassword='xxx',$aDB='intranet') {
$this->host = $aHost;
$this->login = $aLogin;
$this->password = $aPassword;
$this->DB = $aDB;
$this->connect_id = false;
$this->error = false;
}
/*
* connect set the connection with the database
* eg: $DB->connect();
*/
function connect() {
$this->connect_id = mysql_connect($this->host,$this->login,$this->password);
if(!$this->connect_id) {
$this->error = 'Can not connect to the database. Check connection information.';
return false;
}
else {
if(!mysql_select_db($this->DB,$this->connect_id)) {
$this->error = 'You are not allow to access this database or the database "'.$this->DB .'" does not exist.';
return false;
}
else {
return $this->connect_id;
}
}
}
/*
* send a query to the database. $query is a string variable
* eg: DB->sendQuery("SELECT name FROM clients_tbl");
*/
function sendQuery($query) {
if(!$this->connect_id) {
$this->error = 'You are not connected to a MySQL database.';
return false;
}
else if(!$this->result_id = mysql_query($query,$this->connect_id)) {
$this->error = mysql_error();
return false;
}
else {
return $this->result_id;
}
}
/*
* method to use a query previously done
* while ($data = $DB->fetchQuery())
* {
* echo $data['name'] ."<br />\n";
* }
*/
function fetchQuery() {
if(!$this->connect_id) {
$this->error = 'You are not connected to a MySQL database.';
return false;
}
else if(!$this->result_id) {
$this->error = 'You have to do a query to a database to get the number of row in a database.';
return false;
}
else {
return mysql_fetch_assoc($this->result_id);
}
}
/*
* count the number of row of a query
* eg: $DB->numRow()
*/
function numRow() {
if(!$this->connect_id) {
$this->error = 'You are not connected to a MySQL database.';
return false;
}
else if(!$this->result_id) {
$this->error = 'You have to do a query to a database to get the number of row in a database.';
return false;
}
else {
return mysql_num_rows($this->result_id);
}
}
function prepareJumble($string) {
$ready = trim($string);
$ready = preg_replace("#\n|\t|\r#","",$ready);
$ready = strip_tags($ready);
return $ready;
}
/*
* close the MySQL connection
* eg: $DB->close()
*/
function close() {
return mysql_close($this->connect_id) ;
}
}
/*
$ben = new Mysql();
$ben->connect();
$ben->sendQuery('SELECT id,name,firstname FROM people');
while($info = $ben->fetchQuery())
{
$ben2 = new Mysql();
$ben2->connect();
if($ben2->sendQuery('UPDATE intranet SET name='.ucfirst($info['name']).',firstname='.ucfirst($info['firstname']).' WHERE id='.$info['id'].'' )) {
echo 'done';
}
else {
echo 'pb';
}
}
*/
?>
pps: jdéveloppe pour une entreprise anglaise donc désolé si les commentaires sont en anglais