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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    #1

    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 SendFormattedTe xt { ";
    $string = "function boldText() { ";
    $string = " echo \"<b>The world is a good place</b>\" ";
    $string = " } } ";

    eval($string);

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

    ?>

  • Janwillem Borleffs

    #2
    Re: why can't you use eval() to create a class definition?

    lkrubner@geocit ies.com wrote:[color=blue]
    > 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".
    >[/color]

    Not when you use valid code:

    $string = "class SendFormattedTe xt { ";
    $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



    Comment

    • lkrubner@geocities.com

      #3
      Re: why can't you use eval() to create a class definition?

      Thanks for that, I missed the semi-colon.

      [color=blue]
      > But, do yourself a favor and forget that eval exists...[/color]

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

      Comment

      Working...