Page 1 of 1
Check if a variable is reference
Posted: Sat Apr 18, 2009 8:41 am
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?
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 8:47 am
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.
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.
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 8:52 am
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.
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 8:56 am
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?
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 9:20 am
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.
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 9:44 am
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.
Re: Check if a variable is reference
Posted: Sat Apr 18, 2009 11:08 am
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!!!