By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

array datatype problem

P: n/a
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_exists($sKey, $this->$dError)){
$this->$dMessage[$sKey] = $sMessage;
}else{
$this->$dError[$sKey] = true;
$this->$dMessage[$sKey] = $sMessage;
}

}

function isKeyValid($sKey){
$bValid = true;
if(!array_key_exists($sKey,$this->$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>";
}
}
}
}

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mike wrote:
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;


Class properties should be addressed like:

$this->property, not as $this->$property

So the above line becomes:

$this->dError[$sKey] = true;
Apply this modification throughout your code.
JW

Jul 17 '05 #2

P: n/a
JW,
Thanks for the help!

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.