Detecting if your method is being called right of assignment operator?

  • Follow


Is it possible (PHP 5.2) to know inside a function/method/static method if
the value you're going to return is being assigned to something or not?

TIA,
Daniel :)
0
Reply daniel138 (30) 2/2/2009 3:08:05 PM

On Feb 2, 3:08=A0pm, Daniel Smedegaard Buus
<dan...@danielsmedegaardbuus.dk> wrote:
> Is it possible (PHP 5.2) to know inside a function/method/static method i=
f
> the value you're going to return is being assigned to something or not?
>
> TIA,
> Daniel :)

As far as I know there isn't any language where you can do that.  I
also can't think why you would need to know this inside your code
block.
0
Reply gordon.mcvey (380) 2/2/2009 4:15:12 PM


On Mon, 02 Feb 2009 16:08:05 +0100,
Daniel Smedegaard Buus <daniel@danielsmedegaardbuus.dk> wrote:

>Is it possible (PHP 5.2) to know inside a function/method/static method if
>the value you're going to return is being assigned to something or not?

Since you, the programmer, know in advance (presumably!), you
could pass a tip-off argument to the routine whenever you're
using the return value from the particular call.  E.g.

function foo( $a, $b, $using=false ) { ...; return $whatever }

foo( 1, 2 ) ;  // discarding $whatever
$bar = foo( 1, 2, true ) ; // using $whatever
0
Reply anonymously (39) 2/2/2009 6:16:19 PM

A.Reader wrote:

> On Mon, 02 Feb 2009 16:08:05 +0100,
> Daniel Smedegaard Buus <daniel@danielsmedegaardbuus.dk> wrote:
> 
>>Is it possible (PHP 5.2) to know inside a function/method/static method if
>>the value you're going to return is being assigned to something or not?
> 
> Since you, the programmer, know in advance (presumably!)

Well, yes, but this is for a template system, where I'd like to dumb down
some logic so that non-programmers can perform minor programming "magic",
so here _I_ wouldn't know, although the _template "programmer"_ would, so a
similar approach like what you're suggesting is what I'm having now. I was
just wondering if this other approach were possible with PHP :)

As you also say, it appears not, so I guess I'll have to save the "magic"
for some other time ;)

Thanks

> , you 
> could pass a tip-off argument to the routine whenever you're
> using the return value from the particular call.  E.g.
> 
> function foo( $a, $b, $using=false ) { ...; return $whatever }
> 
> foo( 1, 2 ) ;  // discarding $whatever
> $bar = foo( 1, 2, true ) ; // using $whatever

0
Reply daniel138 (30) 2/3/2009 8:23:43 AM

Gordon wrote:

> On Feb 2, 3:08 pm, Daniel Smedegaard Buus
> <dan...@danielsmedegaardbuus.dk> wrote:
>> Is it possible (PHP 5.2) to know inside a function/method/static method
>> if the value you're going to return is being assigned to something or
>> not?
>>
>> TIA,
>> Daniel :)
> 
> As far as I know there isn't any language where you can do that.

You may be right. I was hoping for something in the same league as operator
overloading or reflection. Of course, this would be possible by throwing an
exception and examining the stack trace, but I don't really ever (except
maybe for debugging/error handling purposes) consider that a valid route.

> I 
> also can't think why you would need to know this inside your code
> block.

0
Reply daniel138 (30) 2/3/2009 8:31:28 AM

Daniel Smedegaard Buus escribió:
>>> Is it possible (PHP 5.2) to know inside a function/method/static method
>>> if the value you're going to return is being assigned to something or
>>> not?
>>>
>>> TIA,
>>> Daniel :)
>> As far as I know there isn't any language where you can do that.
> 
> You may be right. I was hoping for something in the same league as operator
> overloading or reflection. Of course, this would be possible by throwing an
> exception and examining the stack trace, but I don't really ever (except
> maybe for debugging/error handling purposes) consider that a valid route.

Well, not really... If you throw the exception inside the function, you 
still haven't assigned the return value. Actually, there isn't a return 
value yet. The stack trace won't display future events ;-)



-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
0
Reply alvaro.NOSPAMTHANX (310) 2/3/2009 8:38:08 AM

"Álvaro G. Vicario" wrote:

> Daniel Smedegaard Buus escribió:
>>>> Is it possible (PHP 5.2) to know inside a function/method/static method
>>>> if the value you're going to return is being assigned to something or
>>>> not?
>>>>
>>>> TIA,
>>>> Daniel :)
>>> As far as I know there isn't any language where you can do that.
>> 
>> You may be right. I was hoping for something in the same league as
>> operator overloading or reflection. Of course, this would be possible by
>> throwing an exception and examining the stack trace, but I don't really
>> ever (except maybe for debugging/error handling purposes) consider that a
>> valid route.
> 
> Well, not really... If you throw the exception inside the function, you
> still haven't assigned the return value. Actually, there isn't a return
> value yet. The stack trace won't display future events ;-)
> 

Hehe, well actually you wouldn't throw the exception to know the return
value, but to determine the caller's context, and that is most definitely
not a future event ;)

0
Reply daniel138 (30) 2/3/2009 10:25:07 AM

Daniel Smedegaard Buus wrote:
> A.Reader wrote:
>
>> On Mon, 02 Feb 2009 16:08:05 +0100,
>> Daniel Smedegaard Buus <daniel@danielsmedegaardbuus.dk> wrote:
>>
>>> Is it possible (PHP 5.2) to know inside a function/method/static
>>> method if the value you're going to return is being assigned to
>>> something or not?
>>
>> Since you, the programmer, know in advance (presumably!)
>
> Well, yes, but this is for a template system, where I'd like to dumb
> down some logic so that non-programmers can perform minor programming
> "magic", so here _I_ wouldn't know, although the _template
> "programmer"_ would, so a similar approach like what you're
> suggesting is what I'm having now. I was just wondering if this other
> approach were possible with PHP :)

What exactly would you do differently if the return value is not assigned to 
something? That is, does it matter? If the return value is not assigned then 
it is merely discarded. Are you thinking of having your function cause some 
side effect?




0
Reply rf 2/3/2009 10:44:58 AM

Daniel Smedegaard Buus escribió:
> "Álvaro G. Vicario" wrote:
> 
>> Daniel Smedegaard Buus escribió:
>>>>> Is it possible (PHP 5.2) to know inside a function/method/static method
>>>>> if the value you're going to return is being assigned to something or
>>>>> not?
>>>>>
>>>>> TIA,
>>>>> Daniel :)
>>>> As far as I know there isn't any language where you can do that.
>>> You may be right. I was hoping for something in the same league as
>>> operator overloading or reflection. Of course, this would be possible by
>>> throwing an exception and examining the stack trace, but I don't really
>>> ever (except maybe for debugging/error handling purposes) consider that a
>>> valid route.
>> Well, not really... If you throw the exception inside the function, you
>> still haven't assigned the return value. Actually, there isn't a return
>> value yet. The stack trace won't display future events ;-)
>>
> 
> Hehe, well actually you wouldn't throw the exception to know the return
> value, but to determine the caller's context, and that is most definitely
> not a future event ;)

All you can get in a backtrace is the source file and line number where 
the function was called. Are you thinking about parsing the PHP code?


-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
0
Reply alvaro.NOSPAMTHANX (310) 2/3/2009 11:20:24 AM

"Álvaro G. Vicario" wrote:

> Daniel Smedegaard Buus escribió:
>> "Álvaro G. Vicario" wrote:
>> 
>>> Daniel Smedegaard Buus escribió:
>>>>>> Is it possible (PHP 5.2) to know inside a function/method/static
>>>>>> method if the value you're going to return is being assigned to
>>>>>> something or not?
>>>>>>
>>>>>> TIA,
>>>>>> Daniel :)
>>>>> As far as I know there isn't any language where you can do that.
>>>> You may be right. I was hoping for something in the same league as
>>>> operator overloading or reflection. Of course, this would be possible
>>>> by throwing an exception and examining the stack trace, but I don't
>>>> really ever (except maybe for debugging/error handling purposes)
>>>> consider that a valid route.
>>> Well, not really... If you throw the exception inside the function, you
>>> still haven't assigned the return value. Actually, there isn't a return
>>> value yet. The stack trace won't display future events ;-)
>>>
>> 
>> Hehe, well actually you wouldn't throw the exception to know the return
>> value, but to determine the caller's context, and that is most definitely
>> not a future event ;)
> 
> All you can get in a backtrace is the source file and line number where
> the function was called.

What you can get in a backtrace are (http://www.php.net/debug_backtrace):

function        string           The current function name. See also __FUNCTION__.
line    integer         The current line number. See also __LINE__.
file    string  The current file name. See also __FILE__.
class   string  The current class name. See also __CLASS__
object  object  The current object.
type    string  The current call type. If a method call, "->" is returned. If
a static method call, "::" is returned. If a function call, nothing is
returned.
args    array   If inside a function, this lists the functions arguments. If
inside an included file, this lists the included file name(s). 

> Are you thinking about parsing the PHP code? 
> 

Yes. But also like I said earlier, "I don't really ever (except maybe for
debugging/error handling purposes) consider that a valid route".

It seems there are a lot of applications out there using such approaches
to "implement" stuff like static late binding to current stable PHP
versions, but it's not anything I'd want to do ;)

0
Reply daniel138 (30) 2/3/2009 12:02:13 PM

On 3 Feb, 12:02, Daniel Smedegaard Buus
<dan...@danielsmedegaardbuus.dk> wrote:
> "=C1lvaro G. Vicario" wrote:
> > Daniel Smedegaard Buus escribi=F3:
> >> "=C1lvaro G. Vicario" wrote:
>
> >>> Daniel Smedegaard Buus escribi=F3:
> >>>>>> Is it possible (PHP 5.2) to know inside a function/method/static
> >>>>>> method if the value you're going to return is being assigned to
> >>>>>> something or not?
>
> >>>>>> TIA,
> >>>>>> Daniel :)
> >>>>> As far as I know there isn't any language where you can do that.
> >>>> You may be right. I was hoping for something in the same league as
> >>>> operator overloading or reflection. Of course, this would be possibl=
e
> >>>> by throwing an exception and examining the stack trace, but I don't
> >>>> really ever (except maybe for debugging/error handling purposes)
> >>>> consider that a valid route.
> >>> Well, not really... If you throw the exception inside the function, y=
ou
> >>> still haven't assigned the return value. Actually, there isn't a retu=
rn
> >>> value yet. The stack trace won't display future events ;-)
>
> >> Hehe, well actually you wouldn't throw the exception to know the retur=
n
> >> value, but to determine the caller's context, and that is most definit=
ely
> >> not a future event ;)
>
> > All you can get in a backtrace is the source file and line number where
> > the function was called.
>
> What you can get in a backtrace are (http://www.php.net/debug_backtrace):
>
> function =A0 =A0 =A0 =A0string =A0 =A0 =A0 =A0 =A0 The current function n=
ame. See also __FUNCTION__.
> line =A0 =A0integer =A0 =A0 =A0 =A0 The current line number. See also __L=
INE__.
> file =A0 =A0string =A0The current file name. See also __FILE__.
> class =A0 string =A0The current class name. See also __CLASS__
> object =A0object =A0The current object.
> type =A0 =A0string =A0The current call type. If a method call, "->" is re=
turned. If
> a static method call, "::" is returned. If a function call, nothing is
> returned.
> args =A0 =A0array =A0 If inside a function, this lists the functions argu=
ments. If
> inside an included file, this lists the included file name(s).
>
> > Are you thinking about parsing the PHP code?
>
> Yes. But also like I said earlier, "I don't really ever (except maybe for
> debugging/error handling purposes) consider that a valid route".
>
> It seems there are a lot of applications out there using such approaches
> to "implement" stuff like static late binding to current stable PHP
> versions, but it's not anything I'd want to do ;)

Wow, using a backtrace and reflection to reverse engineer the
executing code as it runs. Yes, that is a solution to the problem in
the same way that man could have got to the moon by building a really
build ladder.

The problem is in the question - why are you trying to achieve this
end.

Wouldn't it be simpler to not return a value from the function/method
but assign it to a variable passed as a reference?

C.
0
Reply colin.mckinnon (903) 2/3/2009 12:20:11 PM

10 Replies
11 Views

(page loaded in 0.101 seconds)

Similiar Articles:












7/30/2012 3:47:55 AM


Reply: