Req #63834 [PATCH]: Add a function to detect a methods calling context
Edit report at https://bugs.php.net/bug.php?id=63834&edit=1
ID: 63834
Patch added by: krakjoe@php.net
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:
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
Previous Comments:
------------------------------------------------------------------------
[2012-12-31 11:19:32] krakjoe@php.net
I think it makes sense to provide the scope which calls a method. Beyond this is
application specific, I have suggested a patch that provides the name like the
associated methods.
------------------------------------------------------------------------
[2012-12-31 11:11:54] krakjoe@php.net
The following patch has been added/updated:
Patch Name: 63834.patch
Revision: 1356952314
URL: https://bugs.php.net/patch-display.php?bug=63834&patch=63834.patch&revision=1356952314
------------------------------------------------------------------------
[2012-12-22 15:46:44] tolan333 at gmail dot com
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:
<?php
const PHP_CONTEXT_PRIVATE = ?, // Method was called from the class it is defined in.
PHP_CONTEXT_INHERITED = ?, // Method was called from a class inheriting the defining class.
PHP_CONTEXT_EXTERN_USER = ?, // Method was called from outside the class, but in userland code
PHP_CONTEXT_EXTERN_CORE = ?; // Method was called from outside, but was used as callback
(Session handler, error handler, any predefined function awaiting a callback
?>
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:
<?php
public function __set($name, $value)
{
// called from wrong context
if (\property_exists($this, $name)) {
if (\method_exists($this, 'set' . \ucfirst($name))) {
return $this->{'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 this bug report at https://bugs.php.net/bug.php?id=63834&edit=1
Thread (10 messages)