php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #21123 Call Time pass by Reference should not be removed
Submitted: 2002-12-20 17:27 UTC Modified: 2002-12-20 18:33 UTC
From: russ at zerotech dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.3 OS: All
Private report: No CVE-ID: None
 [2002-12-20 17:27 UTC] russ at zerotech dot net
In my opinion I do not think depreciating and removing the Call Time Pass by Reference feature of PHP is a good idea. There are a number of valid reasons to use this feature, to which this feature is the only solution. While programming a very large web database application for my employer, I've found where this feature is extremely useful. Yet when I've upgraded to new versions of PHP I've come to find that it is depreciated. 

My example. In this application I've written a series of classes, I call them XElements. Each XElement is essentially a class that is dedicated to an XHTML 1.0 element.  So for example, I have an XForm, XTable, XTd, XTr, etc.  They all inherit the XElement class. The XElement class has a number of methods that are useful. The most important two are add() and draw().  

The add() method takes one argument. This argument is then put into an array variable of the object, maintaining the order by call to add().  So each time I call add() it adds this argument to the end of the array. What are these arguments? They are the nested CDATA.  Consider this:

<form ...>
    <p>
        Name
        <input ... />
        Password
        <input ... />
     </p>
</form>

The way my code above would work is like this.

$myForm =& new XForm();
$myP =& new XP();
$myForm->add(&$myP);

$myP->add("Name");

$myInput1 =& new XInput();
$myP->add(&$myInput1);

$myP->add("Password");

$myInput2 =& new XInput();
$myP->add(&$myInput2);

$myForm->draw();

This is partially pseudo, since my real constructors have many arguments for attributes of the XHTML code, I've left them out of this example for clarity. Now with that in mind, onto draw().

The draw() method does one simple thing. It iterates through the array of CDATA, and writes it out either by 1) if it's a string, echo it, 2) if it's an object, assume its an XElement and call that XElement's draw(). You can see how it then works. draw() manages the closing tags, and knows which tags are allowed CDATA and which are not thus using /> instead of > as well. 

Now, with the above example we can see where Call Time Pass by Reference comes into play.  If in the first part of my code, I did $myForm->add($myP); instead of $myForm->add(&$myP); that would create a copy of $myP and pass it to add().  Then any calls to $myP after that add() won't reflect the $myP stored in the CDATA Array.  I could modify the declaration of add() so that it takes a reference for the argument, function add(&$arg) { ... }.  However, then I go to do $myP->add("Name");, this would happen:

Fatal error: Cannot pass parameter 1 by reference

So what once would originally have only taken one function, would require two if Call Time Pass by Reference was removed. I'd have to create one function to add non-references, and one to add references.  One could say I could have a function for adding string CDATA, and one for adding XElements, however there comes a time when I don't want to add a reference to a XElement, but rather have it store a copy. This may come into play if say I wanted to construct one XInput then change a field in the object and re-add it over and over, then I would omit the Call Time Pass by Reference.

From what I can gather, removing Call Time Pass by Reference was suppose to make it easier to read code, however if I have to add an additional function to manage non-references, I'm don't see the benefits anymore.
 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-20 17:47 UTC] cynic@php.net
1) you can easily get around this with XForm::addByVal() / XForm::addByRef()
2) I wonder how you got around writing a "very large application" without ever looking in php.ini?


 [2002-12-20 18:04 UTC] russ at zerotech dot net
Apparently you aren't reading my entire comment. First off if you took the time to understand what depreciated means, Call Time Pass by Reference will be removed entirely from PHP. Meaning I can't turn it on in php.ini by setting allow_call_time_pass_reference. That option will no longer exist in future versions of PHP.

Second, yes that is the way around it. And my point exactly. Removing Call Time Pass by Reference was intended to make PHP code a bit cleaner. Well instead of adding one character & to my function, I now have to create an entirely new function to add to my class. So wheres the benefit? No instead of consolidating it into one function I have to split it into two that do 99% the same thing due to the fact Call Time Pass by Reference was removed. I still don't see the reason for removing it at all.
 [2002-12-20 18:09 UTC] russ at zerotech dot net
Oh and I'm surprised by the fact you appear to be a member of the PHP development group yet from a very valid request you take the opportunity to insult me. I've been developing PHP professionly since version 3.0.9.  I think that's about 4 years now.  I've found a lot of the 'staff' very friendly and helpful, Cardinal, Philip, even Rasmus at times. You however, chose to insult me rather than completely understand my request. Thanks, I'm glad I spent all that money on PHP shirts to help support the project.
 [2002-12-20 18:33 UTC] cynic@php.net
my apology for the offence.

i *did* read the whole thing.
PHP is not Perl, you can't squeeze a web application into $^&, neither would doing so make your application more readable.
we apparently have different opinion on what constitutes code-cleanness.

i'm not a member of the PHP development group, i just close a PR every now and then, and that requires a @php.net account.


 [2003-03-27 23:39 UTC] rainmaker at omenmedia dot com
Cynic, I think Russ has a completely valid point here and has stated his case well.  I too have code that requires the use of call time passed references, and changing that code to suit this deprecation is a very difficult task - the only real 'solution' is to duplicate many, many methods, making one for values and one for references.

I actually only discovered the deprecation recently, after trying some code on another server which had the ini setting disabled.  Since then I have been deeply disheartened with the direction PHP seems to be headed.

I still haven't seen a valid reason or explanation anywhere for why call time reference passing is being deprecated.  I agree with Russ in that I think it is a silly idea to remove it, because shouldn't the decision of whether something gets passed by value OR reference be up to the application developer when creating the application?

The idea of creating seperate functions or methods just to handle values and references seperately is ludicrous and surely represents a far greater annoyance than using a few ampersands here and there?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 09 23:01:32 2024 UTC