note 81013 added to function.debug-backtrace

From: Date: Mon, 11 Feb 2008 08:04:05 +0000
Subject: note 81013 added to function.debug-backtrace
Groups: php.notes 
Request: Send a blank email to php-notes+get-136023@lists.php.net to get a copy of this message
Here's a way to get the arguments for an upstream function in your stack (works with class methods, static methods and non-class methods): <?php /** * getArgs - find arguments of upstream method * can be called with, e.g. "funcname", "class::staticmethod", "class->instancemethod". */ function getArgs( $target, $subclass_ok = true ) { if( strpos( $target, "::" ) ) { list( $class, $target ) = explode( "::", $target, 2 ); $type = "::"; } else if( strpos( $target, "->" ) ) { list( $class, $target ) = explode( "->", $target, 2 ); $type = "->"; } else { $type = NULL; $class = NULL; } $class and $class = new ReflectionClass( $class ); foreach( debug_backtrace() as $obj ) { if( $obj['function'] == $target ) { if( $type and $obj['type'] == $type ) { $_cl = new ReflectionClass( $obj['class'] ); if( $_cl->getName() == $class->getName() or ( $subclass_ok and $_cl->isSubclassOf( $class ) ) ) { return $obj['args']; } unset( $_cl ); } else if( !$type ) { return $obj['args']; } } } return NULL; } ?> Some example usage: <?php class Foo { function test() { $args = getArgs( "Foo->base" ); print( "the parameter 'v' to my call of base was: {$args[0]}\n" ); } function base( $v ) { $this->test(); } } $f = new Foo(); $f->base( 713 ); // will print.. ".. my call of base was: 713" ?> Trust me, there are some reasons for why you might want to do this :) ---- Server IP: 203.202.10.60 Probable Submitter: 121.44.77.58 ---- Manual Page -- http://www.php.net/manual/en/function.debug-backtrace.php Edit -- https://master.php.net/note/edit/81013 Del: integrated -- https://master.php.net/note/delete/81013/integrated Del: useless -- https://master.php.net/note/delete/81013/useless Del: bad code -- https://master.php.net/note/delete/81013/bad+code Del: spam -- https://master.php.net/note/delete/81013/spam Del: non-english -- https://master.php.net/note/delete/81013/non-english Del: in docs -- https://master.php.net/note/delete/81013/in+docs Del: other reasons-- https://master.php.net/note/delete/81013 Reject -- https://master.php.net/note/reject/81013 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136023) next »