Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Actually most of the code initializes Fructose, in PHP I can write the same code like (6 lines, 7 including blank line):

  class Greeter {
      public static function greet($text) {
          echo $text;
      }
  }

  Greeter::greet("Hello, World!\n");


That code isn't the same as the original. The instance variable is being initialized via a block -- your function needs to accept a callback.


This would be the PHP that'd do the same thing:

    class Greeter {
        public function __construct($fn) {
            $this->greeting = $fn();
        }
        public function greet() {
            echo $this->greeting;
        }
    }
    $greeter = new Greeter(function() {
        return "Hello, World!";
    });
    $greeter->greet();
That code will only work in PHP 5.3. If you want anonymous functions in lower versions, you're going to have to use create_function(). If you want closures, you may as well forget about doing it nicely.

There's a reason the Fructose generated PHP isn't very pretty. The Fructose generated code will run on any version of PHP that's at least 5.0.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: