PHP4 Reference Troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

PHP4 Reference Troubles

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

foo($string = 'string');
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Apparently, that works, although:

Code: Select all

function foo(&$bar) {$bar = 'new';}

foo($string = 'old');

echo $string; // old
lampdevelopers
Spammer :|
Posts: 3
Joined: Mon Feb 12, 2007 1:15 am

Php code help

Post by lampdevelopers »

u can find php code related help from here LINK REMOVED
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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]$
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Ah... well, at least we know not to rely on that behavior.
Post Reply