Find out where custom PHP function is defined - php

I use internal function function_exists, that can return true but I can't find the custom function in my project. I also debugged my code to trace the function, but the debugger does not step into my custom function. I very look forward to know why. Please help me, thank you.

To find out where is the function defined, use following code:
<?php
$rf = new ReflectionFunction('my_fuction_name');
echo 'file:' . $rf->getFileName() . ', line:' . $rf->getStartLine();
?>
Please note that if the function is not defined in source code, but it's internal PHP function, both getFileName() and getStartLine() will return false.
You can check if the function is internal that way:
<?php
$rf = new ReflectionFunction('my_fuction_name');
if($rf->isInternal() === TRUE){
echo "Function is internal!";
}else{
echo "Function is not internal.";
}
?>

Related

php function in array broken

I am trying to setup an array that pulls the filename and function name to run, but it not fully working.
The code is
$actionArray = array(
'register' => array('Register.php', 'Register'),
);
if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) {
echo '<br><br>index<br><br>';
echo 'test';
exit;
}
require_once($actionArray[$_REQUEST['action']][0]);
return $actionArray[$_REQUEST['action']][1];
Register.php has
function Register()
{
echo 'register';
}
echo '<br>sdfdfsd<br>';
But it does not echo register and just sdfdfsd.
If I change the first lot of code from
return $actionArray[$_REQUEST['action']][1];
to
return Register();
It works, any ideas?
Thanks
Change the last line to:
return call_user_func($actionArray[$_REQUEST['action']][1]);
This uses the call_user_func function for more readable code and better portability. The following also should work (Only tested on PHP 5.4+)
return $actionArray[$_REQUEST['action']][1]();
It's almost the same as your code, but I'm actually invoking the function instead of returning the value of the array. Without the function invocation syntax () you're just asking PHP get to get the value of the variable (in this case, an array) and return it.
You'll find something usefull here:
How to call PHP function from string stored in a Variable
Call a function name stored in a string is what you want...

Joomla module and variables within functions

I have created a module in Joomla and all is working fine, but when I put a function in and try and access a variable it does not work, but if I echo it outside the function it is ok
$item_img = $params->get('item_img','modules/mod_k2_mobile/images/item_icon.gif');
// not working
function GetIMG(){
global $item_img;
echo "item".$item_img;
}
GetIMG();
// working
echo "item".$item_img;
why?
I would advice you not to use any global variables as long as you can avoid it. Can't you change your function declaration to something like "GetIMG( $item_img )"?
If you still want to use a global variable, this should work:
**global $item_img;**
$item_img = $params->get('item_img','modules/mod_k2_mobile/images/item_icon.gif');
// not working
function GetIMG(){
global $item_img;
echo "item".$item_img;
}
GetIMG();
// working
echo "item".$item_img;
I hope it helps!

Improve current PHP function to handle errors

if someone could help me to improve this function to use it with this format (from scratch, not tested):
<?php
define("LINEA", __LINE__, true);
function line($string)
{
return $string . LINEA;
}
echo line('Error: ');
?>
Example of current use:
<?php
function line($startText, $line, $endText = NULL)
{
$endText = !empty($endText) ? $endText : '';
return $startText . $line . $endText;
}
/*
...
lot of code
...
*/
echo line('Error on line: ', __LINE__) . '<br />';
/*
...
lot of code
...
*/
echo line('Alert - ', __LINE__, '!');
?>
Outputs:
Error on line: 12
Alert - 18!
You might consider using debug_backtrace to obtain information about the function caller, including line, file, class, the current scope, and much much more. This way you don't need to pass any of the line number information into your error logging function.
Be aware that generating this information can be somewhat of a drag on performance.
You should also consider using an existing logging package, like PEAR's Log, Zend_Log, or Apache log4php.
It doesn't look like your line() function is doing you any good any more. Why don't you just call:
echo 'Error on line: ' . __LINE__;

Tracking function instances

OK so is there a way in php do track function calls such as
function Tracker($name,$returnedValue,$file,$line)
{
echo $name . '() was called and returned a ' . typeof(returnedValue);
}
function test(){}
test();
The reason for this is to send a custom framework data type back so another example would be
$resource = fopen('php://stdin'); //This would return an instance of (Object)Resource.
if($resource->type == 'fopen')
{
//Code
}
I have never seen anyway to do this but does anyone know if it is possible ?
It's not possible to do this using just PHP, a debugger might help, however you could wrap the function:
function wrapper()
{
$args=func_get_args();
$function=array_shift($args);
$returned=call_user_func_array($function, $args);
print "$function (" . var_export($args, true) . ") = "
. var_export($returned, true) . "\n";
return $returned;
}
$value=wrapper('test_fn', 1 ,2 ,3, 'something');
$value=wrapper('mysql_connect');
I don't understand your explanation of what you are trying to achieve here.
C.
Not really. Xdebug is able to log function calls though: http://xdebug.org/docs/execution_trace
Maybe you want something like Observer pattern?
http://en.wikipedia.org/wiki/Observer_pattern

Zend Framework view script, functions and variable scope

I am rather new to ZendFramework and am trying to figure this out. In my view script (index.phtml), I have this bit of code that says:
<?php
function getErrorString($element)
{
echo "<pre>";
print_r($this);
echo "</pre>";
$string = '';
if(!empty($this->error[$element]))
{
$string = $string.'<label class="error" for="'.$element.'" generated="true">';
foreach($this->error[$element] as $error)
{
$string = $string.$error;
}
$string = $string.'</label>';
}
return $string;
}
echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString("blah");
die();
That gives me:
Fatal error: Using $this when not in object context in index.phtml on line XX
It seems to me that when you create a function within a view, you lose the $this variable. I did search around the net, and I can't see anyone else trying to achieve what I am doing (highly unlikely, maybe I'm searching it wrong).
With past experience developing other apps, I can't see a good reason why this function should be placed in a separate helper -> especially since this is the only place the function will ever be called.
Any ideas would be greatly appreciated.
Your function getErrorString() isn't an objectmethod of the Zend_View-Object.
It has it's own scope and couldn't reach $this.
The following code should work for you in the index.phtml
function getErrorString($viewObject, $element)
{
echo "<pre>";
print_r($viewObject);
echo "</pre>";
$string = '';
if(!empty($viewObject->error[$element]))
{
$string = $string.'<label class="error" for="'.$element.'" generated="true">';
foreach($viewObject->error[$element] as $error)
{
$string = $string.$error;
}
$string = $string.'</label>';
}
return $string;
}
echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString($this,"blah");
die();
The last use of "$this" variable is probably the main reason for showing the fatal error. It's quite justified because of the fact that you cannot write anything else in the class definition, except defining methods & properties with respect to that class.
Also if you are creating any function in a view page, then within that function the "$this" variable is not accessible by default. So you will have to make that "$this" variable go global or you need to print the required part, related to "$this" variable, outside the function definition.
echo "<pre>";
print_r($this);
echo "</pre>";
So when you are writing the above code in the function definition, the PHP Parser is unable to find any object context for this "$this" variable. It's not that you are losing that "$this" variable, but it will not be accessible, but to the missing logic.
Hope it helps.

Categories