开发者

Is there anything like CoffeeScript for PHP?

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. D开发者_如何学Coes anything like this exist?


I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!


The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.

Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.

JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)

All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.


Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.


If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.

I've only ever used it for Flash but found it very useful.


If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.


@gosukiwi made Blueberry, which looks like this:

/* 
 I'm a multiline comment
*/

a = 1 # variable definition

# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }

if a == 1
  echo("Hello, World!")
end

for i in (0..10)
  echo(i)
end

class MyClass < MyParentClass
  @name

  def Greet
    echo("Hello! My name is " & @name)
  end
end

They also mentioned it in this comment.


Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.

I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.

https://github.com/c9s/coffeephp


Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)


This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor


Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜