473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why can't you use eval() to create a class definition?


We are probably lucky that PHP doesn't allow this, but I'm curious
about what the argument is against allowing this? Why did the inventors
of PHP keep this from working? This same thing, using eval to create a
class defintion, works in Javascript, demonstrating, perhaps, that
Javascript is very flexible and you can do awful things with it.

Anyway, the following code prints out "class does not exist".
<?php

// 05-26-05 - just a personal demo to satify my personal curiosity to
see if eval() will
// create a class definition

$string = "class SendFormattedText { ";
$string = "function boldText() { ";
$string = " echo \"<b>The world is a good place</b>\" ";
$string = " } } ";

eval($string);

if (class_exists("SendFormattedText")) {
$obj = new SendFormattedText();
$obj->boldText();
} else {
echo "<p>class does not exist ";
}

?>

Jul 17 '05 #1
2 3026
lk******@geocities.com wrote:
We are probably lucky that PHP doesn't allow this, but I'm curious
about what the argument is against allowing this? Why did the
inventors of PHP keep this from working? This same thing, using eval
to create a class defintion, works in Javascript, demonstrating,
perhaps, that Javascript is very flexible and you can do awful things
with it.

Anyway, the following code prints out "class does not exist".


Not when you use valid code:

$string = "class SendFormattedText { ";
$string .= "function boldText() { ";
$string .= " echo \"<b>The world is a good place</b>\";";
$string .= "}}";

eval($string);
But, do yourself a favor and forget that eval exists...
JW

Jul 17 '05 #2
Thanks for that, I missed the semi-colon.

But, do yourself a favor and forget that eval exists...


But I don't know how to get a template to render PHP commands without
eval(). I get a template from a database as a string and it has PHP
commands in it. How can I get it to work without eval()?

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Steward | last post by:
I'm trying to create a generic object that will work for several different types of databases. Does anyone know why this won't work? // Connect to the database $cmd = $this->db_type . "_connect...
4
by: Jean-Sébastien Bolduc | last post by:
Hello, I would like to associate a local namespace with a lambda function. To be more specific, here is exactly what I would want: def foo(): a = 1 f = lambda x : a*x return f
3
by: Kevin Smith | last post by:
I want to evaluate an expression using eval(), but I only want to supply the variable values "on demand." For example, let's say that I want to evaluate 'x+y'. Normally, I would create a...
5
by: Andrea Williams | last post by:
I'm looking for an equivalent to the JavaScript function 'eval'. Anyone know one that will work in C#? I'm guessing that it doesn't exist due to strong data typing, but thought I'd ask. Thx!...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
3
by: Michal Lipinski | last post by:
Hi its my first post. I have a problem, I want to user eval() function in a for loop to set labels to staticText so i done something like this: ...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.