Page 1 of 1
reusing code
Posted: Thu Apr 19, 2007 10:35 am
by timbrown
Hi,
I am reusing code using include() / require() but I find that things like "return" and the "__FUNCTION__" don't work in the normal way because the scope is limited to the include file.
Is there a way of storing code in a text file, and have it evaluated as code inline with the other code, as if it were actually part of the other code?
Thanks in advance,
Tim.
Posted: Thu Apr 19, 2007 10:44 am
by timvw
Have a look at
http://www.php.net/eval
(notice that you would need to make sure the parts with php code also need an opening <?php and closing ?> tag...)
(be warned that blind use of eval is a source of security problems...)
Posted: Thu Apr 19, 2007 10:54 am
by timbrown
Thanks for getting back.
I played around with eval() and sadly it seems to operate in the same way.
example
Code: Select all
function tester(){
eval("echo __FUNCTION__;");// this echoes nothing
eval("return false;") // this does not halt execution of the function
echo "hello"; // this is still echoed because the return did not happen as usual
}
Posted: Thu Apr 19, 2007 11:09 am
by Kieran Huggins
code reuse... isn't that what functions and classes are for?
What are you trying to do exactly Tim?
Posted: Thu Apr 19, 2007 12:18 pm
by timbrown
Kieran Huggins wrote:code reuse... isn't that what functions and classes are for?
What are you trying to do exactly Tim?
Thanks for getting back,
I am trying to use the following code in several functions -
Code: Select all
<?php
$this->log[] = __FUNCTION__" caused an error";
return false;
?>
this code works fine when TYPED into the function, but when you try to use it in an include file __FUNCTION__ does not work at all and return halts the execution of the include file ONLY, it does not halt the execution of the containing function.
Posted: Thu Apr 19, 2007 2:49 pm
by Kieran Huggins
how are you including the file? can you post an actual example?
Posted: Thu Apr 19, 2007 3:43 pm
by John Cartwright
debug_backtrace() might be of interest