1

How to implement this kind of functionality:

  • Fill entity eg. Member with data
  • Bind Member to form with $form->bind($member) to private property _formData
  • Afterward do some stuff inside $form, eg. $form->validate() with _formData
  • $member should be also changed as _formData is changed.

    class Form {
    
        private $_formData;
    
        function bind1(&$row) {
            // this change member outside
            $row['full_name'] =
                $row['first_name']
                . ' ' .
                $row['last_name'];
        }
    
        function bind2(&$row) {
            $this->_formData = $row;
            // this will not change memeber
            $this->_formData['full_name'] =
                $this->_formData['first_name']
                . ' '
                . $this->_formData['last_name'];
        }
    }
    
    $member = array('full_name' => null, 'first_name'=>'Fn', 'last_name' => 'Ln');
    $form = new Form();
    
    $form->bind1($member);
    var_dump($member['full_name']);
    // output: 'FnLn'
    
    $form->bind2($member);
    var_dump($member['full_name']);
    // output: null
    

Method validate work with private _fieldData, so this to work bind2 test should work.

2 Answers 2

0

What you are trying to do is possible, but you need to set a reference of the reference in the bind1 and bind2 method, like this:

$this->_formData = & $row;

You are also making misspellings between full_name and fullName as array keys. For example in the bind2 method:

$this->_formData['full_name'] =  $this->_formData['first_name'] . ' ' . $this->_formData['last_name'];

And in your test-code you var_dump full_name. Chaging full_name in bind2 to fullName should fix your issue.

1
  • yep! $this->_formData = &$row; solved. Assigning private _formData with pass by reference also **&**$row is solution that worked. Wanted to self-answer but this way is nicer :) Other part is not important (misspelling) - just test code. Oct 24, 2014 at 16:42
0

the problem is you are assigning the full_name key of your member variable and trying to access fullName variable so it is returning NULL

1
  • Sorry about that. This part was just misspelled in question code. You are right about null, thanx. Oct 24, 2014 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.