Re: FormBuilder::useCallTimePassByReference
| From: | Justin Patrin | Date: | Thu, 10 Mar 2005 21:17:40 +0000 |
| Subject: | Re: FormBuilder::useCallTimePassByReference | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17954@lists.php.net to get a copy of this message | ||
On Thu, 10 Mar 2005 10:23:15 -0800, Brian Luft <brian@tablevision.net> wrote:
>
>
> Hello,
>
> Can someone help me make sense of what is going on here? I have a
> DataObject for which I defined a postGenerateForm callback:
>
> class DataObjects_User_FormSignIn extends DB_DataObject
>
> {
>
> ###START_AUTOCODE
>
> .
>
> ###END_AUTOCODE
>
> var $fb_fieldsToRender = array( 'username', 'pwd' );
>
> var $fb_fieldLabels = array(
>
> 'username' =>
> 'Username',
>
> 'pwd' => 'Password'
>
> );
>
> function postGenerateForm( &$form )
>
> {
>
> ..change a property or two on the form.
>
> print_r( $form );
>
> }
>
> }
>
> I can see from the print_r that the changes I make to form are there. Then
> I get my form object
>
> $builder = &DB_DataObject_FormBuilder::create( $myDataObject );
>
> $form = $builder->getForm();
>
> Now if I do a print_r on the form object my changes are lost - unless I
> modify the line in FormBuilder.php responsible for calling the postGen
> callback to:
>
> $this->_do->postGenerateForm(&$obj, $this);
>
> So it seems like I am supposed to set useCallTimePassByReference to TRUE but
> I haven't seen people doing this in the few examples I've found or really
> mention it. My allow_call_time_pass_reference is set to 'On' and I would
> prefer to keep it this way since my hosted production environment uses this
> setting. I have been around the programming block but am pretty new to PHP
> so it could be that I'm not understanding PHP references properly. I
> thought that "function postGenerateForm( &$form )" meant that $form would
> get passed by reference into the function. Can anyone help me to understand
> why the form var is being passed by value to my callback unless I tweak
> FormBuilder?
>
This is a huge mess in PHP, not your fault at all. It's due to
overloading (which is severely broken in PHP4). Pass by reference is
completely broken if you overload your objects (which DB_DataObject
does by default). To fix this, put this line before you include
DB/DataObject.php:
define('DB_DATAOBJECT_NO_OVERLOAD', 1);
You can also set useCallTimePassByReference to true in FormBuilder to
fix this, but I suggest stopping overloading instead.
Search the archives for more.
--
Justin Patrin