Req #63834 [Com]: Add a function to detect a methods calling context

2013-05-21 Thread tolan333 at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=63834&edit=1

 ID: 63834
 Comment by: tolan333 at gmail dot com
 Reported by:tolan333 at gmail dot com
 Summary:Add a function to detect a methods calling context
 Status: Open
 Type:   Feature/Change Request
 Package:Class/Object related
 Operating System:   Any
 PHP Version:Irrelevant
 Block user comment: N
 Private report: N

 New Comment:

Thanks for the comments. After rethinking about this, krakjoe's suggestions 
seems to be superior (solves the issue and is way easier). Is their any chance 
to see this improvement in 5.5 or 5.6? Or what needs to be done to get this 
implemented?


Previous Comments:

[2012-12-31 12:02:45] krak...@php.net

acquiring a backtrace is an inefficient means of obtaining this information ...


[2012-12-31 11:55:17] ni...@php.net

I still fail to use just what exactly this asks for and how it would be 
beneficial.

@krakjoe: The get_calling_method and get_calling_class functions you added 
should already be fully covered by debug_backtrace, so I see little value in 
adding them (as the use case is rather limited).

@op: Regarding the last two PHP_CONTEXT_EXTERN constants, what do you mean by 
"user" and "core"? E.g. if you invoke a callback using call_user_func, is that 
an internal or a userland call? It's the internal function doing the call, but 
it's really the user who triggers it. I don't see how these constants would 
carry any meaning.

The other two again can be covered by debug_backtrace, can't they? Just get the 
class of the call and check whether it equals __CLASS__ (=> private) or is a 
subclass of __CLASS__ (=> protected) or is none (=> public). Seems simple 
enough to me.


[2012-12-31 11:49:59] krak...@php.net

The following patch has been added/updated:

Patch Name: 63834-2.patch
Revision:   1356954599
URL:
https://bugs.php.net/patch-display.php?bug=63834&patch=63834-2.patch&revision=1356954599


[2012-12-31 11:37:55] krak...@php.net

-2 will provide get_calling_method and get_calling_class, I think that's 
everything you should need


[2012-12-31 11:36:48] krak...@php.net

The following patch has been added/updated:

Patch Name: 63834-2.patch
Revision:   1356953808
URL:
https://bugs.php.net/patch-display.php?bug=63834&patch=63834-2.patch&revision=1356953808




The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

https://bugs.php.net/bug.php?id=63834


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63834&edit=1


[PHP-BUG] Req #63834 [NEW]: Add a function to detect a methods calling context

2012-12-22 Thread tolan333 at gmail dot com
From: tolan333 at gmail dot com
Operating system: Any
PHP version:  Irrelevant
Package:  Class/Object related
Bug Type: Feature/Change Request
Bug description:Add a function to detect a methods calling context

Description:

Currently it is hard to get to know the exact context from which a method
is called from. Sure, there is debug_backtrace and the Reflection API, but
these are no ideal nor complete and reliable solutions.

I suggest to introduce a new function: get_calling_context (similar to
get_calling_class).

A possible use-case is shown below

Possible return values could be:



Use case:
Currently I use virtual properties (via __get and __set) to validate and
manipulate properties data while still keeping the ability to iterate over
the entire object(implementing IteratorAggregate). This allows me also to
have different visibility states for accessors (which will hopefully be
supported in 5.5 without having to rely on custom implementations) but not
for the properties themself.

Simplified __set implementation:
{'set' . \ucfirst($name)}($value);
} else {
throw new WritePropertyFromWrongContextException("virtual
property $name can not be accessed from this context");
}
} elseif (\method_exists($this, 'set' . \ucfirst($name))) {
return $this->{'set' . \ucfirst($name)}($value);
} elseif ($this->objectConfiguration['accessMapAsProps'] == true &&
$this->offsetExists($name)) {
$this[$name] =
$this->createPropertyValidator($name)->validate($value,$this->getRuleSet()[$name])->getValidatedValue();
} else {
throw new WriteNonExistingPropertyException("Virtual property
\$$name does not exist in class " .
\get_class($this));
}
}
?>

There is no possibility to react on different scenarios as there can be
only one __set which is either public,protected or private. There is no
option to implement different behaviors for different visibility.

Another usecase is the usage of object callbacks in handlers like
session.set.save.handler

For example the write callback does not (per documentation) output data.
However in debug scenarios and in unit-tests it would be ideal to know if
the method was called from the core as a usual session handler or in a
different scenario in usercode.


Additionally this would go inline with already existing functions like
get_called_class, get_parent_class and partly get_class.



-- 
Edit bug report at https://bugs.php.net/bug.php?id=63834&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63834&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63834&r=trysnapshot53
Try a snapshot (trunk): 
https://bugs.php.net/fix.php?id=63834&r=trysnapshottrunk
Fixed in SVN:   https://bugs.php.net/fix.php?id=63834&r=fixed
Fixed in release:   https://bugs.php.net/fix.php?id=63834&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=63834&r=needtrace
Need Reproduce Script:  https://bugs.php.net/fix.php?id=63834&r=needscript
Try newer version:  https://bugs.php.net/fix.php?id=63834&r=oldversion
Not developer issue:https://bugs.php.net/fix.php?id=63834&r=support
Expected behavior:  https://bugs.php.net/fix.php?id=63834&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=63834&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=63834&r=submittedtwice
register_globals:   https://bugs.php.net/fix.php?id=63834&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63834&r=php4
Daylight Savings:   https://bugs.php.net/fix.php?id=63834&r=dst
IIS Stability:  https://bugs.php.net/fix.php?id=63834&r=isapi
Install GNU Sed:https://bugs.php.net/fix.php?id=63834&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63834&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=63834&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63834&r=mysqlcfg