使用方法参数绑定外部变量和Class受保护字段

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.

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.

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