Hello, I'm attempting to create class in PHP that basically manages a
two seperate arrays. However, when I attempt to add a new element to
the array, the array itself converts to the datatype of the element I'm
attempting to add instead of being an array.
I am probably overlooking something very obvious, but here is my code.
The conversion takes place on this line:
$this->$dError[$sKey] = true;
in the method
addError($sKey, $sMessage)
Here is my source for the class:
class FormErrors {
var $dError;
var $dMessage;
var $errTxtClass;
var $errTxtLeft;
var $errTxtRight;
function FormErrors(){
$this->$dError = array(0 => true);
$this->$dMessage = array(0 => true);
}
function addError($sKey, $sMessage){
if(array_key_ex ists($sKey, $this->$dError)){
$this->$dMessage[$sKey] = $sMessage;
}else{
$this->$dError[$sKey] = true;
$this->$dMessage[$sKey] = $sMessage;
}
}
function isKeyValid($sKe y){
$bValid = true;
if(!array_key_e xists($sKey,$th is->$dError)){
$bValid = false;
}
return $bValid;
}
function isValid(){
$bValid = true;
foreach($this->$dError as $sKey => $bValue){
if($bValue){
$bValid = false;
}
}
return $bValid;
}
function printErrors(){
foreach($this->$dError as $sKey => $bValue){
if($bValue){
echo $sKey."<br>";
}
}
}
}
two seperate arrays. However, when I attempt to add a new element to
the array, the array itself converts to the datatype of the element I'm
attempting to add instead of being an array.
I am probably overlooking something very obvious, but here is my code.
The conversion takes place on this line:
$this->$dError[$sKey] = true;
in the method
addError($sKey, $sMessage)
Here is my source for the class:
class FormErrors {
var $dError;
var $dMessage;
var $errTxtClass;
var $errTxtLeft;
var $errTxtRight;
function FormErrors(){
$this->$dError = array(0 => true);
$this->$dMessage = array(0 => true);
}
function addError($sKey, $sMessage){
if(array_key_ex ists($sKey, $this->$dError)){
$this->$dMessage[$sKey] = $sMessage;
}else{
$this->$dError[$sKey] = true;
$this->$dMessage[$sKey] = $sMessage;
}
}
function isKeyValid($sKe y){
$bValid = true;
if(!array_key_e xists($sKey,$th is->$dError)){
$bValid = false;
}
return $bValid;
}
function isValid(){
$bValid = true;
foreach($this->$dError as $sKey => $bValue){
if($bValue){
$bValid = false;
}
}
return $bValid;
}
function printErrors(){
foreach($this->$dError as $sKey => $bValue){
if($bValue){
echo $sKey."<br>";
}
}
}
}
Comment