PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun May 31, 2020 4:54 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat Apr 18, 2009 8:41 am 
Offline
Forum Commoner

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


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 8:47 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.
Syntax: [ Download ] [ Hide ]
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.


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 8:52 am 
Offline
Forum Commoner

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


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 8:56 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
<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?


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 9:20 am 
Offline
Forum Commoner

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


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 9:44 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Sat Apr 18, 2009 11:08 am 
Offline
Forum Commoner

Joined: Tue Apr 14, 2009 6:20 am
Posts: 35


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group