I want to create a class for a license manager , however , I have to use another class to do what I need is to create a new connection to the database in the __construct class .
When I try to use a function , getAll in this case , I have a PHP error that appears on the screen :
Fatal error: Using $ this WHEN NOT in object context in [...] line 112 .
Here is my class :
class cLicences {
// Contient les valeurs à afficher
protected $iLicID = null;
protected $sLicProduct = null;
protected $sLicDesc = null;
protected $sLicKey = null;
protected $iLicDateAdded = null;
protected $iLicDateEdited = null;
protected $iLicUserAdded = null;
protected $iLicUserEdited = null;
// Connexion à la base de données
private $oLicMySQL = null;
// Contient les noms des champs dans la db
private static $sFieldID = 'id_licence';
private static $sFieldProduct = 'lic_product';
private static $sFieldDesc = 'lic_desc';
private static $sFieldKey = 'lic_key';
private static $sFieldDateAdded = 'lic_date_added';
private static $sFieldDateEdited = 'lic_date_edited';
private static $sFieldUserAdded = 'idx_user_added';
private static $sFieldUserEdited = 'idx_user_edited';
// Nom de la table
const TABLENAME = 't_licences';
/**
* Initialisation de l'objet
* @param int $iLicID -> ID de la licence
*/
public function __construct($iLicID = null) {
// Si l'ID est un ID (numérique donc)
if(is_numeric($iLicID)) {
// Stock la connexion dans la classe
$this->oLicMySQL = new cMySQLi(DB_USERNAME,DB_PASSWORD,DB_HOSTNAME,DB_HOSTPORT,DB_DATABASE);
// Initialise l'ID de la classe avec l'ID passé en param
$this->iLicID = $iLicID;
// Initialise les variables de la classe selon l'ID
$this->init();
}
}
/**
* Détruit la connexion à la db
*/
public function __destruct() {
$this->oLicMySQL->fClose();
}
/**
* Retourne la valeur souhaitée selon le nom du champ en param
* @param string $sName -> Nom du champs
* @return string
*/
public function __get($sName) {
// Retourne la valeur souhaitée
return $this->$sName;
}
/**
* Initalise les variables de la classe avec les données reçuent selon l'ID
*/
public function init() {
// Séléctionne la licence selon ID
$sReqSltLic = 'SELECT *
FROM '.self::TABLENAME.'
WHERE '.self::$sFieldID.' = "'.$this->iLicID.'"
LIMIT 1';
// Exécute la requête
$rLic = $this->oLicMySQL->fQuery($sReqSltLic);
// Met en forme la ressource SQL
$aLic = $this->oLicMySQL->fFetch($rLic);
// Assigne aux variables de la class les données reçuent
$this->sLicProduct = $aLic[self::$sFieldProduct];
$this->sLicDesc = $aLic[self::$sFieldDesc];
$this->sLicKey = $aLic[self::$sFieldKey];
$this->iLicDateAdded = $aLic[self::$sFieldDateAdded];
$this->iLicDateEdited = $aLic[self::$sFieldDateEdited];
$this->iLicUserAdded = $aLic[self::$sFieldUserAdded];
$this->iLicUserEdited = $aLic[self::$sFieldUserEdited];
}
/**
* Récupère toutes les licences
* @param ressource SQL $rData -> & indique l'index mémoire de la variable
* @param string $sArg -> Argument pour la séléction des licences
* @return \cLicences
*/
public function getAll(&$rData,$sArg = null) {
if(empty($rData)) {
$sReqSltAll = 'SELECT *
FROM '.self::TABLENAME.(!empty($sArg) ? ' '.$sArg : null);
$rData = $this->oLicMySQL->fQuery($sReqSltAll);
}
// On met en forme les données
$aData = $this->oLicMySQL->fFetch($rData); <--- LINE 112
// Si on a des données
if(!empty($aData)) {
// Parcours les données
while($sValue = $aData) {
// Retourne un nouvel objet licence
return new cLicences($sValue[self::$sFieldID]);
}
}
}
}
Here is my page:
// Affiche les erreurs
error_reporting(E_ALL);
// Change le dossier courant
chdir('../../../');
// Fichier requis pour traitement
require_once('./configuration/general.conf.php');
require_once(PATH_CONFIGURATION.'user.conf.php');
require_once(PATH_CLASS.'mysqli.class.php');
require_once(PATH_MODULES.'licencesManager/langages/'.SITE_LANG.'.lang.php');
require_once(PATH_MODULES.'licencesManager/classes/licences.class.php');
// Nouvelle connexion à la db
$oMySQL = new cMySQLi(DB_USERNAME,DB_PASSWORD,DB_HOSTNAME,DB_HOSTPORT,DB_DATABASE);
// Initialise la classe licence
$oLicences = new cLicences();
// Récupération de toutes les licences
$sReqSelectLicence = "SELECT id_licence
FROM t_licences";
$rLicences = $oMySQL->fQuery($sReqSelectLicence);
// Affiche les infos de la licence
while($oLicence = cLicences::getAll($rLicences)) {
echo '<tr>
<td>'.$oLicence->iLicID.'</td>
<td>'.$oLicence->sLicProduct.'</td>
<td>
<button onclick="fShowLicence('.$oLicence->iLicID.')" class="btn btn-xs btn-primary">
<i class="fa fa-key hidden-xs"></i> '.$_SESSION['mdlLicShow'].'
</button>
</td>
<td><i class="fa fa-pencil"></i> <i class="fa fa-trash-o"></i></td>
</tr>';
}
// Ferme la connexion à la base de données
$oMySQL->fClose();
Here is the full error:
Fatal error: Using $this when not in object context in /srv/www/htdocs/dev/php/TEST/DAS/iRat-tools/modules/licencesManager/classes/licences.class.php on line 112
I 'm stuck on this problem for 3 days now. If anyone can help me to find a solution.