array datatype problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    array datatype problem

    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>";
    }
    }
    }
    }

  • Janwillem Borleffs

    #2
    Re: array datatype problem

    Mike wrote:[color=blue]
    > 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;
    >[/color]

    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



    Comment

    • Mike

      #3
      Re: array datatype problem

      JW,
      Thanks for the help!

      Comment

      Working...