converting php4 to php5 problem
Posted: Sun May 10, 2009 5:25 am
Hello,
i'm a bit new at using classes in PHP. When i tried to migrate a site from version 4 to 5 the site did not work anymore. It has probably to do with the use of globals and the use of the this-pseudo variable. Can somebody help me??
this is part of the code i'm using
extra functions
The class that is causing the trouble
i'm a bit new at using classes in PHP. When i tried to migrate a site from version 4 to 5 the site did not work anymore. It has probably to do with the use of globals and the use of the this-pseudo variable. Can somebody help me??
this is part of the code i'm using
Code: Select all
<?php
/* Library met de abstracte klassen voor de database layer */
class AbstractDBObject {
var $_fields;
var $_field_names;
var $_errors;
var $_required;
var $_require_errors;
var $_new;
var $_db_name;
var $_db_table_name;
var $_id_field;
var $_error_email;
function AbstractDBObject() {
global $cfg;
$this->_db_name = $cfg['db_name'];
$this->_fields = array();
$this->_field_names = array();
$this->_errors = array();
$this->_required = array();
$this->_require_errors = array();
$this->_new = true;
$this->_error_email = $cfg['error_email'];
}
function loadById($id) {
$q = "SELECT * FROM ".$this->_db_name.".".$this->_db_table_name." WHERE ".$this->_id_field." = '".$id."'";
$res = mysql_query($q);
if(mysql_num_rows($res) == 1) {
$this->_fields = mysql_fetch_assoc($res);
$this->_new = false;
return true;
} else {
return false;
}
}
function store()
{
global $cfg;
if(count($this->_errors))
{
return false;
}
if(! $this->_new)
{
foreach($this->_field_names as $field )
{
if(isset($this->_fields[$field]))
$pairs .= $field." = '".addslashes($this->_fields[$field])."', ";
else
$pairs .= $field." = NULL, ";
}
$pairs = substr($pairs,0,-2);
$q_head = "UPDATE ".$this->_db_name.".".$this->_db_table_name." SET ";
$q_foot = " WHERE ".$this->_id_field." = '".$this->_fields[$this->_id_field]."'";
$q = $q_head.$pairs.$q_foot;
if(mysql_query($q))
{
return true;
}
else
{
mail ($this->_error_email,'vriendstap error','QUERY: '.$q.' | ERROR: '.mysql_error());
}
}
else
{
foreach($this->_fields as $k => $v) {
$names .= $k.",";
$values .= "'".addslashes($v)."',";
}
$names = substr($names,0,-1);
$values = substr($values,0,-1);
$q = "INSERT INTO ".$this->_db_name.".".$this->_db_table_name."(".$names.") VALUES(".$values.")";
if(mysql_query($q)) {
$this->_fields[$this->_id_field] = mysql_insert_id();
return true;
}
else
mail ($this->_error_email,'vriendstap error','QUERY: '.$q.' | ERROR: '.mysql_error());
}
return false;
}
function getFields() {
return $this->_fields;
}
}
?>extra functions
Code: Select all
<?php
function __fo($num) {
return (strlen($num) == 1) ? '0'.$num : $num;
}
function __add_standard_criteria( &$criteria) {
global $this, $sdata;
$criteria[] = "verwijderd != 1";
}
function __add_search_criteria(&$criteria) {
global $this, $sdata;
if ($sdata['filter']['form']['naam']) {
$criteria[] = "(voornaam LIKE '%".$sdata['filter']['form']['naam']."%' OR "
."achternaam LIKE '%".$sdata['filter']['form']['naam']."%' OR "
."tussenvoegsel LIKE'%".$sdata['filter']['form']['naam']."%')";
}
if ($sdata['filter']['form']['county'])
{
$criteria[] = sprintf("UPPER(provincie) LIKE UPPER('%s')", $sdata['filter']['form']['county']);
}
if ($sdata['filter']['form']['email'])
{
$criteria[] = sprintf("email LIKE '%%%s%%'", $sdata['filter']['form']['email']);
}
}
?>The class that is causing the trouble
Code: Select all
<?php
require_once($env['doc_root'].'/_lib/db/abstractdb.lib.php');
require_once($env['doc_root'].'/_lib/db/search_sql.php');
/* Gebruiker DBObject klasse */
class Gebruiker extends AbstractDBObject {
function Gebruiker() {
global $env;
parent :: AbstractDBObject(); //call de parent constructor
$this->_db_table_name = $env['db_tables']['gebruikers'];
$this->_field_names = array(
'id',
'email',
'wachtwoord',
'voornaam',
'tussenvoegsel',
'achternaam',
'admin_status',
'gebruiker_status',
'aanmelddatum',
'geldig_tot',
'betaaldatum',
'verwijderd'
);
$this->_id_field = 'id';
$this->_required = array(
1 => 'email',
2 => 'voornaam',
3 => 'achternaam'
);
$this->_require_errors = array(
1 => 'U moet een e-mail adres opgeven',
2 => 'U moet een voornaam opgeven',
3 => 'U moet een achternaam opgeven'
);
}
/* Afgezien van de standaard loadById kan een gebruiker ook bij login geladen worden */
function loadByLogin($email, $pass)
{
$tempExpiryDate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') - 14, date('Y')));
$q = "SELECT * FROM ".$this->_db_name.".".$this->_db_table_name." WHERE email = '".addslashes($email)."'
AND wachtwoord = '".md5($pass)."'
AND ((admin_status = 'enabled' AND geldig_tot > NOW()) OR (admin_status = 'proef' AND aanmelddatum >= '" . $tempExpiryDate . "'))
AND verwijderd != 1";
$res = mysql_query($q);
if(mysql_num_rows($res) == 1) {
$this->_fields = mysql_fetch_assoc($res);
return true;
}
$q = "SELECT * FROM ".$this->_db_name.".".$this->_db_table_name."
WHERE email = '".addslashes($email)."' AND wachtwoord = '".md5($pass)."'";
$res = mysql_query($q);
if(mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
if($row['admin_status'] == 'proef' && $row['aanmelddatum'] <= $tempExpiryDate && $row['verwijderd'] != 1)
{
$this->_errors[] = 'Uw proefperiode is verlopen, neem contact op met de beheerder';
}
else
{
$this->_errors[] = 'Gegevens zijn correct maar u hebt geen toegang meer, neem contact op met de beheerder';
}
return false;
} else
{
$this->_errors[] = 'Gebruikersnaam of wachtwoord is onjuist';
return false;
}
}
}
?>