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

This is a discussion on why can't you use eval() to create a class definition? within the PHP Language forums, part of the PHP Programming Forums category; We are probably lucky that PHP doesn't allow this, but I'm curious about what the argument is against ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-27-2005
lkrubner@geocities.com
 
Posts: n/a
Default 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 ";
}

?>

Reply With Quote
  #2 (permalink)  
Old 05-27-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: why can't you use eval() to create a class definition?

lkrubner@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



Reply With Quote
  #3 (permalink)  
Old 05-28-2005
lkrubner@geocities.com
 
Posts: n/a
Default Re: why can't you use eval() to create a class definition?

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()?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:32 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0