Check if a variable is reference

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
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Check if a variable is reference

Post by Pulni4kiya »

Hi!
Is there a way to check whether a variable is a reference?

That's what I need to do:

function a($b) {
if (is_reference($b)) { ... }
}

I know that call-time pass-by-reference is deprecated but it's still allowed so I need to check this. Any ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Check if a variable is reference

Post by requinix »

Why does it matter?

As far as I'm aware, no, you can't know. Not at runtime. But since pass-by-reference isn't allowed anymore, the only way to get a reference is to say it in the function signature.

Code: Select all

function a(&$b)
Since you're writing the code that's inside that function, you already know whether it's a reference or not.

I think you know all that already, but I'm just pointing it out.
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Check if a variable is reference

Post by Pulni4kiya »

It's still allowed, just deprecated. Which means that someone who's using my class might be passing arguments this way. And I don't like to disallow something that is still allowed by the language.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Check if a variable is reference

Post by requinix »

<opinion>
If they're doing something wrong then it's their fault. They should be getting warnings that they're doing it, and if they hide those errors then it's by their own actions. You can't protect yourself against stupid developers.
</opinion>

But the question still stands: why does it matter if a variable is by-value or by-reference?
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Check if a variable is reference

Post by Pulni4kiya »

My class uses these parameters to call another method which is not mine.

And since I don't want to disallow call-time pass-by-ref, I have to pass the params to the other method as references (even if they're not passed to mine by-ref).
But the user might not want that (he might have passed them by-value to my method), so I had to use @ to hide the warning (since it's not user's fault) but it hides warnings in the other method I call which is also wrong. The warnings there are user's fault.
So I need to know which args are by-ref.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Check if a variable is reference

Post by requinix »

Fact of the matter is you can't know.

Look. At some point you have to put some responsibility on the guy using your code. You can't try to pander to everybody's preferences because it'll only drive you crazy.
You can make your method flexible but at some point you have to draw the line.
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Check if a variable is reference

Post by Pulni4kiya »

I managed to do it without checking whether it's passed by-ref or by-value.

I'm passing the arguments to the other method in an eval. Now I'm passing all args by-ref and changing error_reporting before the eval() and returning it to what it was on the first line of the eval()'ed code. This way the warning is omitted but the error reporting is returned before the function call so any errors in user's code are shown (that depends on the previous error_reporting level).

Do you think this is an acceptable solution?

Code: Select all

function a($a) {
    $a = 'CHANGED!!!';
}
 
$h = error_reporting(E_ERROR);
eval('
    error_reporting(' . $h . ');
    $b = 123;
    a(&$b);
    a(); //WARNING
');
echo $b, '<br/>'; //CHANGED!!!
Post Reply