SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP: Class instance used by class instance behaves odd

    I have created a FormBuilder classpackage, but the following weird problem occured when implementing.

    In the FieldSet class, as seen below, there is a method called attachElement(), used to attach input fields, select boxes and the like to the fieldset. I found out that the $elements array used to stash those objects gets emptied every time the method gets executed. The moment the final create() method is called, no form elements exist in that array.

    Additional info:
    - I create those forms by instantiating a SimpleForm object, that handles the attaching and creation of input fields and fieldsets. However, when I create a new FieldSet() directly, instead of doing this through SimpleForm, the array does NOT get emptied and works fine. This is confusing, does this problem rest in the FieldSet class or SimpleForm class ? (SimpleForm code added below)
    - On my development platform everything behaves correctly.
    PHP versions:
    development platform: 5.1.2
    live server: 4.4.2
    - InputFields that are NOT added to a fieldset but are created directly execute properly.

    *** note ***
    I'd rather avoid discussion about my coding practice and wether or not I did a good job programming a Form Builder package. I will truly love all feedback, but for now it's more important to solve this problem.

    Thanks in advance

    FieldSet :

    PHP Code:
    class FieldSet
    {
        
    // string containing the starting tag of the fieldset
        
    var $startingTag;
        
    // array containing the different formelement instances
        
    var $elements;
        
    // string containing the closing tag of the fieldset
        
    var $closingTag;        
        
    // name of the fieldset
        
    var $name;
            
        
    /**
        *    class constructor
        *    @access    public
        *    @param    string    text contained in the legend element of this fieldset instance
        *    @param    string    additional headers for the fieldset tag
        */
            
        
    function FieldSet ($name,$legend,$additionalAttributes=''){
            
    /* instantiate array */
            
    $this->elements = array();
            
    /* create opening and closing tags */
            
    $this->startingTag "<fieldset id=\"SF".$name."_fieldset\">\n<legend>$legend</legend>\n";
            
    $this->closingTag "</fieldset>";
            
    $this->name $name;
        }
            
        
    /**
        *    adds an element to the elements array
        *    @access public
        *    @param    object    instance of a form element class
        */
            
        
    function attachElement ($el) {
            
    array_push($this->elements,$el);
        }
            
        
    /**
        *    returns the name of the fieldset
        */
            
        
    function getName () {
            return 
    $this->name;
        }
            
        
    /**
        *    creates the markup code for the form
        */
            
        
    function create () {
            
    $markup "";
            
    $markup .= $this->startingTag "\n";
            foreach (
    $this->elements as $element) {
                
    $markup .= $element->create() . "\n";
            }
            
    $markup .= ($this->closingTag "\n");
            return 
    $markup;
        }

    SimpleForm :

    PHP Code:
    class SimpleForm
    {    
        
    // array keeping track of the form elements added to the form
        
    var $formElements;
        
    // string keeping the complete form markup. Printed by the create() method
        
    var $markUp;
        
    // string the starting tag of the form
        
    var $startingTag;
        
    // string the closing tag of the form
        
    var $closingTag;
            
        
    /**
        *    class constructor
        *    @access    public
        *    @param    string    name of the form
        *    @param    string    method of the form
        *    @param    string    action of the form
        *    @param    string    attional attributes for the form tag
        */
            
        
    function SimpleForm ($name$method$action$additionalAttributes='') {
            
    /* instantiate arrays */
            
    $this->formElements = array();
            
    /* save opening tag of form */
            
    $this->markUp "";
            
    $this->startingTag "<form method=\"$method\" action=\"$action\" name=\"$name\" id=\"SF_".$name."\" $additionalAttributes>\n";
            
    $this->closingTag '</form>';
        }
            
        
    /**
        *    method    addField()
        *    adds fieldsets to the form
        *
        *    @access public
        *    @param    string    legend of fieldset
        *    @param    string    additional attributes for the fieldset tag
        */
            
        
    function addField ($name$legend$additionalAttributes='') {
            
    array_push($this->formElements,new FieldSet($name$legend$additionalAttributes));
        }
            
        
    /**
        *    method addInput()
        *    adds a basic input textfield
        *
        *    @access    public
        *    @param    string    label for input field
        *    @param    string    name of input field
        *    @param    string    name of the fieldset this input field belongs to
        *    @param    string    mode, can either be left empty for normal input fields, or "hidden" or "password"
        *    @param    string    class of the input field
        *    @param    string    additional attributes
        */
            
        
    function addInput ($label$name$fieldSetName=''$mode='text'$class=''$additionalAttributes='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == "") {
                
    // add element to the FormElements array
                
    array_push($this->formElements,new InputField($label,$name,$mode,$class,$additionalAttributes));
            } else {
                
    // if fieldset is chosen, walk trough array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {    
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new InputField($label,$name,$mode,$class,$additionalHeaders));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new InputField($label,$name,$mode,$class,$additionalHeaders)); 
                        break;
                    }
                }
            }
        }
            
        
    /**
        *    method addSubmit()
        *    adds a submit button to the form
        *
        *    @param    string    text for the value attribute of the submit button
        *    @param    string    specifying if the submit button is of the 'image' type or the regular 'submit' type
        *    @param    string    url of the image, required if this button is in image mode
        *    @param    string    name of the fieldset this submit button belongs to
        *    @param    string    the name of the submit button
        *    @param    string    the class of the submit button
        *    @param    string    additional attributes for the submit button
        */
            
        
    function addSubmit ($value$fieldSetName=''$mode='submit'$src=''$name=''$class=''$additionalAttributes='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == "") {
                
    // add element to the FormElements array
                
    array_push($this->formElements,new SubmitButton($value$mode$src$name$class$additionalAttributes));
            } else {
                
    // if fieldset is chosen, walk trough array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {    
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new SubmitButton($value$mode$src$name$class$additionalAttributes));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new SubmitButton($value$mode$src$name$class$additionalAttributes)); 
                        break;
                    }
                }
            }
        }
        
        
    /**
        *    method addTextArea()
        *    adds a textarea to the form
        *
        *    @param    string    label for the textarea
        *    @param    string    name of the textarea
        *    @param    string    number of columns of the textarea
        *    @param    string    number of rows of the textarea
        *    @param    string    name of the fieldset this textarea belongs to
        *    @param    string    class name of the textarea
        *    @param    string    additional attributes for the textarea
        */
            
        
    function addTextArea ($label$name$cols$rows$fieldSetName=''$class=''$additionalAttributes='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == "") {
                
    // add element to the FormElements array
                
    array_push($this->formElements,new TextArea($label$name$cols$rows$class$additionalAttributes));
            } else {
                
    // if fieldset is chosen, walk trough array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {    
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new TextArea($label$name$cols$rows$class$additionalAttributes));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new TextArea($label$name$cols$rows$class=''$additionalAttributes)); 
                        break;
                    }
                }
            }
        }
            
        
    /**
        *    method addRadio()
        *    adds a radio button to the form
        *
        *    @param    string    label of the radio button
        *    @param    string    name of the radio button
        *    @param    boolean    specifying wether the radio button is checked or not
        *    @param    string    name of the fieldset this radio button belongs to
        *    @param    string    the class of the radio button
        *    @param    string    additional attributes for the radio button
        */
            
        
    function addRadio ($label$name$value$checked=FALSE$fieldSetName=''$class=''$additionalAttributes='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == "") {
                
    // add element to the formElements array
                
    array_push($this->formElements,new RadioButton($label,$name$value$checked,$class,$additionalAttributes));
            } else {
                
    // if fieldset is chosen, walk through array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new RadioButton($label$name$value$checked$fieldSetName$class$additionalAttributes));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new RadioButton($label$name$value$checked$fieldSetName$class$additionalAttributes));
                        break;
                    }
                }
            }
        }
            
        
    /**
        *    method addCheckBox()
        *    adds a checkbox to the form
        *
        *    @param    string    label of the checkbox
        *    @param    string    name of the checkbox
        *    @param    boolean    specifying wether the checkbox is checked or not
        *    @param    string    name of the fieldset this checkbox belongs to
        *    @param    string    the class of the checkbox
        *    @param    string    additional attributes for the checkbox
        */
            
        
    function addCheckBox ($label$name$checked=FALSE$fieldSetName=''$class=''$additionalAttributes='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == "") {
                
    // add element to the formElements array
                
    array_push($this->formElements,new CheckBox($label,$name$value$checked,$class,$additionalAttributes));
            } else {
                
    // if fieldset is chosen, walk through array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new CheckBox($label$name$value$checked$fieldSetName$class$additionalAttributes));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new CheckBox($label$name$value$checked$fieldSetName$class$additionalAttributes));
                        break;
                    }
                }
            }
        }
            
        
    /**
        *    method addHTMLElement()
        *    adds HTML elements to the form, other than form elements
        *
        *    @param    string    HTML code for this tag
        *    @param    string    name of the fieldset this element belongs to
        */
        
        
    function addHTMLElement ($tag,$fieldSetName='') {
            
    // if no fieldset is chosen
            
    if ($fieldSetName == '') {
                
    // add element to the formElements array
                
    array_push($this->formElements,new HTMLElement($tag));
            } else {
                
    // if fieldset is chosen, walk through array
                
    foreach ($this->formElements as $el) {
                    
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {
                        
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new HTMLElement($tag));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements, new HTMLElement($tag));
                        break;
                    }
                }
            }
        }
            
        
    /**
        *    method create()
        *    prints the HMTL-code marking up the form
        *
        *    @access    public
        */
        
        
    function create () {
            
    $this->markUp .= $this->startingTag;
            foreach (
    $this->formElements as $el) {
                
    $this->markUp .= $el->create();
            }
            
    $this->markUp .= $this->closingTag;
            echo 
    $this->markUp;
        }


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the FieldSet class, as seen below, there is a method called attachElement(), used to attach input fields, select boxes and the like to the fieldset. I found out that the $elements array used to stash those objects gets emptied every time the method gets executed.
    That doesn't happen with php 4.4:
    Edit:

    Additional info:
    - I create those forms by instantiating a SimpleForm object, that handles the attaching and creation of input fields and fieldsets. However, when I create a new FieldSet() directly, instead of doing this through SimpleForm, the array does NOT get emptied and works fine.
    oops. Back to the drawing board.

    PHP Code:
    class FieldSet
    {
        
    // string containing the starting tag of the fieldset
        
    var $startingTag;
        
    // array containing the different formelement instances
        
    var $elements;
        
    // string containing the closing tag of the fieldset
        
    var $closingTag;        
        
    // name of the fieldset
        
    var $name;
            
        
    /**
        *    class constructor
        *    @access    public
        *    @param    string    text contained in the legend element of this fieldset instance
        *    @param    string    additional headers for the fieldset tag
        */
            
        
    function FieldSet ($name,$legend,$additionalAttributes=''){
            
    /* instantiate array */
            
    $this->elements = array();
            
    /* create opening and closing tags */
            
    $this->startingTag "<fieldset id=\"SF".$name."_fieldset\">\n<legend>$legend</legend>\n";
            
    $this->closingTag "</fieldset>";
            
    $this->name $name;
        }
            
        
    /**
        *    adds an element to the elements array
        *    @access public
        *    @param    object    instance of a form element class
        */
            
        
    function attachElement ($el) {
            
    array_push($this->elements,$el);
        }
            
        
    /**
        *    returns the name of the fieldset
        */
            
        
    function getName () {
            return 
    $this->name;
        }
            
        
    /**
        *    creates the markup code for the form
        */
            
        
    function create () {
            echo 
    count($this->elements) . '<br/>';
            foreach(
    $this->elements as $element)
            {
                echo 
    $element->getName() . '<br/>';
            }

            
    $markup "";
            
    $markup .= $this->startingTag "\n";
          
              foreach (
    $this->elements as $element) {
                
    $markup .= $element->create() . "\n";
            }
           
            
    $markup .= ($this->closingTag "\n");
            return 
    $markup;
        }


    class 
    TestObj
    {
        var 
    $name;
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        function 
    create()
        {
            echo 
    'Create function called' '<br/>';
        }
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $fs = new FieldSet("one""some text");
    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $fs->attachElement($to1);
    $fs->attachElement($to2);

    $temp $fs->create(); 
    output:
    Code:
    2
    test1
    test2
    Create function called
    Create function called

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this foreach loop here:
    PHP Code:
    foreach ($this->formElements as $el) {
        
    // if the element in array is a fieldset
        
    if (is_a($el,'FieldSet')) {    
            
    // and its name corresponds to the one chosen
            
    if ($el->getName() == $fieldSetName) {
                
    // add to fs
                
    $el->attachElement (new InputField($label,$name,$mode,$class,$additionalHeaders));
                break; 
    $el is a copy of each object stored in the formElements array. So when you attach an element to the object, it attaches the element to the copy--not the object in the array. I would guess that in php5, a foreach loop obtains a reference to the original element of the array instead of a copy. You can test that with some code like this:
    PHP Code:
    class TestObj
    {
        var 
    $name;
        
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        
        function 
    setName($name)
        {
            
    $this->name $name;
        }
        
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $arr[] = $to1;
    $arr[] = $to2;

    foreach(
    $arr as $to)
    {
        
    $to->setName("Sally");
    }
    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';

    php4 output:
    Code:
    test1
    test2

  4. #4
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does that have to do with using php references ?
    Can I solve this by adding an ampersand (which I'm gonna test in a minute) and if so, is this good coding practice?

  5. #5
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been playing around with ampersands, but I keep getting the syntax error "Unexpected & found [...]" et cetera.

    I'm not really familiar with references, could you provide me with some guidelines?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I checked the manual on a foreach loop, and in both php4 and php5 foreach operates on a copy, so I don't know why you are seeing different behavior.

    I've been playing around with ampersands, but I keep getting the syntax error "Unexpected & found [...]" et cetera.
    foreach

    PHP 4 introduced a foreach construct...

    As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

    <?php
    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
    $value = $value * 2;
    }
    // $arr is now array(2, 4, 6, 8)
    ?>

  7. #7
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    I checked the manual on a foreach loop, and in both php4 and php5 foreach operates on a copy, so I don't know why you are seeing different behavior.
    This doesn't make a difference. I get an empty screen on my live server.
    error_reporting ('E_ALL'); doesn't show any errors (but this could be caused by the php.ini file of my provider of course).

    What I did:

    - changed the addInput() method:
    PHP Code:
    foreach ($elements as &$el) { 
    [...] 
    - tried to create a form like this:
    PHP Code:
    $sf = new SimpleForm('testForm1','post',$_SERVER['PHP_SELF']);
    $sf->addField ('set1','Vul uw gegevens in');
    $sf->addInput ('Foo','Bar','set1');
    $sf->create(); 

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ize
    This doesn't make a difference. I get an empty screen on my live server.
    On my development platform everything behaves correctly.
    PHP versions:
    development platform: 5.1.2
    So, do you get the same behavior in php 4 and 5 or different behavior?
    Using a for-loop instead of a foreach loop will work:
    PHP Code:
    class TestObj
    {
        var 
    $name;
        
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        
        function 
    setName($name)
        {
            
    $this->name $name;
        }
        
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $arr[] = $to1;
    $arr[] = $to2;

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';
    }

    $count count($arr);
    for(
    $i=0$i $count$i++)
    {
        
    $arr[$i]->setName("fluffy");
    }

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';


  9. #9
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    So, do you get the same behavior in php 4 and 5 or different behavior?
    Using a for-loop instead of a foreach loop will work:
    PHP Code:
    class TestObj
    {
        var 
    $name;
        
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        
        function 
    setName($name)
        {
            
    $this->name $name;
        }
        
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $arr[] = $to1;
    $arr[] = $to2;

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';
    }

    $count count($arr);
    for(
    $i=0$i $count$i++)
    {
        
    $arr[$i]->setName("fluffy");
    }

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';

    The difference in behaviour is that php5 works like a charm, php5 doesn't do a thing

    But I will indeed change to a for loop and see how that goes.

  10. #10
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm, looks like a for loop creates a copy too... darn.

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Run the following code with php4 and php5 and post the results:
    PHP Code:
    class TestObj
    {
        var 
    $name;
        
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        
        function 
    setName($name)
        {
            
    $this->name $name;
        }
        
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $arr[] = $to1;
    $arr[] = $to2;

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';
    }

    foreach(
    $arr as $to)
    {
        
    $to->setName("fluffy");
    }

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';

    Then run this code with php4 and php5 and post the results:
    PHP Code:
    class TestObj
    {
        var 
    $name;
        
        function 
    TestObj($name)
        {
            
    $this->name $name;
        }
        
        function 
    setName($name)
        {
            
    $this->name $name;
        }
        
        function 
    getName()
        {
            return 
    $this->name;
        }
    }

    $to1 = new TestObj("test1");
    $to2 = new TestObj("test2");
    $arr[] = $to1;
    $arr[] = $to2;

    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';
    }

    $count count($arr);
    for(
    $i=0$i $count$i++)
    {
        
    $arr[$i]->setName("fluffy");
    }


    foreach(
    $arr as $to)
    {
        echo 
    $to->getName() . '<br/>';


  12. #12
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Test 1, php5 output:

    test1
    test2
    fluffy
    fluffy
    Test 1, php4 output:

    test1
    test2
    test1
    test2
    Test 2, php5 output:

    test1
    test2
    fluffy
    fluffy
    Test 2, php4 output:

    test1
    test2
    fluffy
    fluffy

  13. #13
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. from the looks of that I'd say a for-loop should do the job...
    But changing my addInput() method to

    PHP Code:
    function addInput ($label$name$fieldSetName=''$mode='text'$class=''$additionalAttributes='') {
        
    // if no fieldset is chosen
        
    if ($fieldSetName == "") {
            
    // add element to the FormElements array
            
    array_push($this->formElements,new InputField($label,$name,$mode,$class,$additionalAttributes));
        } else {
            
    // if fieldset is chosen, walk trough array
            
    $l count($this->formElements);
            for (
    $i 0$i $l$i++) {
                
    $el $this->formElements[$i];
                
    // if the element in array is a fieldset
                    
    if (is_a($el,'FieldSet')) {
                    
    // and its name corresponds to the one chosen
                        
    if ($el->getName() == $fieldSetName) {
                            
    // add to fs
                            
    $el->attachElement (new InputField($label,$name,$mode,$class,$additionalAttributes));
                            break;
                        }
                    } else {
                        
    // if no element in the array is a fieldset, attach element to array
                        
    array_push($this->formElements,new InputField($label,$name,$mode,$class,$additionalAttributes)); 
                        break;
                    }
                }
            }
        } 
    doesn't solve anything...

    Edit:
    Ah, it does!
    By saving $this->formElements[$i] in a new variable I was again making a copy

    I want to thank you very much, 7stud. I can finally start using this package in my projects.

  14. #14
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By saving $this->formElements[$i] in a new variable I was again making a copy
    If you still want to do that for the purposes of code readability or so that you don't have to type as much, you can do this:
    PHP Code:
    $el =& $this->formElements[$i]; 
    After that statement, $el and $this->formElements[$i] will refer to the same object. The & tells php not to make a copy of the object but instead assign a 'reference' to the object to $el. You end up with this:

    Code:
            an object
          /           \
         /             \
        /               \
    $el             $this->formElements[$i]
    Both variable names refer to the same object.

  15. #15
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    If you still want to do that for the purposes of code readability or so that you don't have to type as much, you can do this:
    PHP Code:
    $el =& $this->formElements[$i]; 
    After that statement, $el and $this->formElements[$i] will refer to the same object. The & tells php not to make a copy of the object but instead assign a 'reference' to the object to $el. You end up with this:

    Code:
            an object
          /           \
         /             \
        /               \
    $el             $this->formElements[$i]
    Both variable names refer to the same object.
    Cool
    Thanks again for all your help. If I were to debug this alone, it had cost me a looong time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •