0

Lets say I want to save function in database. For example we have table function with attributes: function and function name. For saving function for time we would save it in data base:

time_funtion(){ return time() } and "Time".

I have no idea how to execute this function after reading from DB is this even possible?

I could do this with files - read from database then write it in file, then include file and calling this function will work. Any other ideas?

6
  • 8
    seems like a misguided workaround for a misunderstood problem .. when answer is "use eval", you are asking the wrong question Commented May 22, 2012 at 12:36
  • possible duplicate of String variable to execute PHP code Commented May 22, 2012 at 12:38
  • 2
    I think it would be a good idea for you to try to explain in a few words why would you even consider implementing something so nasty and error-prone. Maybe we can suggest a workaround, that will enable you to write robust code, without having to rely on something so evil. Just imagine that somebody else comes after you to modify this code... Commented May 22, 2012 at 12:50
  • @Luka, @tereško is correct. Use eval function and pass your code as parameter to it. Commented May 22, 2012 at 12:54
  • @Imdad , what is complete opposite of what i meant Commented May 22, 2012 at 13:02

3 Answers 3

2

I'm not sure why you'd want to do this but you could use eval()

Sign up to request clarification or add additional context in comments.

Comments

2

You can use eval() for this. But be careful what you save into your db. There are just a few problems where this is a useful approach. In 99% you don't need this and it's lacking security, because you are executing unkown code, which is not what you want.

Comments

1

Use eval("your PHP code here")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.