Page 1 of 1
PHP4 Reference Troubles
Posted: Sun Feb 11, 2007 8:49 am
by Chris Corbyn
Code: Select all
Fatal error: Cannot pass parameter 1 by reference in /Users/d11wtq/public_html/swiftmailer2/branches/php4/v3_2/tests/units/testcases/TestOfMime.php on line 27
I have a function which works beautifully in PHP5. It's supposed to be flexible so it does (slightly) different things depending upon whether you give it an object (of known type), or just a string (which is what the object would generate itself!).
In PHP5, I don't need to think about the use of the reference operator very much for method declarations because they sorted out the OO model a heck of a lot, so I simply did something like this:
Code: Select all
public functon foo($input)
{
if ($input instanceof Something) { /* do one thing*/ }
else { /* cast as (string) and do something else */ }
}
Now, it's
critical that if an object was passed, it's passed by reference because I do some fairly complex sutff with nodes and moving them around deeper inside the library - something I'd rather not bore you with.
Now that I'm converting the code to PHP4 I have to declare the method like this:
Code: Select all
public functon foo(&$input)
{
if (is_a($input, "Something")) { /* do one thing*/ }
else { /* cast as (string) and do something else */ }
}
However, that generates the error you see above if I try to pass it a string directly. Any hints? I'm considering just making two different methods, but that's not desirable because I don't want to differ my API too much between PHP5 and PHP4.
Posted: Sun Feb 11, 2007 8:56 am
by feyd
What about varying the call itself?
Code: Select all
$bar->foo(&$myObject);
// and
$bar->foo($myString);
You do have to worry about
allow_call_time_pass_reference however.
Posted: Sun Feb 11, 2007 9:02 am
by Chris Corbyn
That should work, thanks

Funnily enough I tried it a min ago and my script hung in an infinite loop so I convinced myself I'd lost my marbles and posted here. More likely I just changed too much code

Posted: Sun Feb 11, 2007 9:11 am
by feyd
A two method one can work as well. The secondary method in PHP 5 could simply redirect to the normal method whereas in PHP 4, it would actually do its own work.
Posted: Sun Feb 11, 2007 11:55 am
by Ambush Commander
Remember that you can always fudge around it by using:
Code: Select all
$string = 'string';
foo($string);
// rather than
foo('string');
I often do this for functions that return by reference and can return a null. Rather than return null, I return $null. Of course, it's a little verbose.
Posted: Sun Feb 11, 2007 1:02 pm
by Weirdan
Posted: Sun Feb 11, 2007 1:49 pm
by Ambush Commander
Apparently, that works, although:
Code: Select all
function foo(&$bar) {$bar = 'new';}
foo($string = 'old');
echo $string; // old
Php code help
Posted: Mon Feb 12, 2007 5:25 am
by lampdevelopers
u can find php code related help from here LINK REMOVED
Posted: Mon Feb 12, 2007 5:36 am
by Chris Corbyn
If you've come to this forum to keep posting links your website where they are not relevant then I suggest you stop.
Posted: Tue Feb 13, 2007 1:27 pm
by Weirdan
Ambush Commander wrote:Apparently, that works, although:
Code: Select all
function foo(&$bar) {$bar = 'new';}
foo($string = 'old');
echo $string; // old
Hmm... interesting
Code: Select all
[weirdan@server]$ php -r 'echo phpversion() . "\n"; function foo(&$bar) {$bar = "new";} foo($string = "old"); echo $string . "\n";'
4.4.3RC2-dev
new
[weirdan@server]$ php5 -r 'echo phpversion() . "\n"; function foo(&$bar) {$bar = "new";} foo($string = "old"); echo $string . "\n";'
5.1.6
old
[weirdan@server]$
Posted: Tue Feb 13, 2007 2:32 pm
by Ambush Commander
Ah... well, at least we know not to rely on that behavior.