reusing code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

reusing code

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...)
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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

}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

code reuse... isn't that what functions and classes are for?

What are you trying to do exactly Tim?
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

how are you including the file? can you post an actual example?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

debug_backtrace() might be of interest
Post Reply