New language on top of PHP?

PhpProgramming LanguagesLanguage Design

Php Problem Overview


I'm a PHP developer. I like PHP! It is a really good language if you know how to use it, but I know it allows very bad design sometimes.

It reminds me of JavaScript which has good parts and bad parts. One particular project, CoffeeScript, tries to focus only on the good parts, forcing you to write good code.

I was thinking if something similar could be done with PHP... A new syntax that would be compiled only to good PHP code taking advatage of all the new and exciting stuff we can get with PHP 5.3.

So, getting ahead of some people, I'll ask: Why create a new language on top of PHP if you can just use Ruby or Python or something else?

  • PHP is easy to deploy anywhere
  • The language itself has a lot of good features and ideas
  • There are lots of good libraries written in PHP
  • ...

So, my real questions here are...

  • Is this a stupid idea? Why would it be? Do you think CoffeeScript is stupid?
  • How do someone starts to create a new language on top of another? I know nothing about this, but I would like to learn. Where to start?

Php Solutions


Solution 1 - Php

The idea is definitely not stupid, especially if executed well.

I like coffeescript a lot, but it has it's approach has downsides as well. Debugging a coffeescript script still requires you read the generated Javascript code, which can be tedious, since you haven't written it actually yourself.

I've understood that Jeremy Ashkenas, the creator of coffeescript has started to work on coffeescript after reading "Create your own freaking awesome programming language" by Marc-André Cournoyer.

Good luck!

Solution 2 - Php

The reason CoffeScript is a good idea is that if developers want to run code in a client browser they have to use javascript; so the only way to program in a different language is to allow that language to be convertible to javascript.

I'm not sure the same really applies to server side programming. If you've got issues with PHP and want to use a new language there is no real advantage to having that language generate PHP.

On the other hand, a language that was very similar to PHP, but fixed some of the flaws would be a great idea.

Solution 3 - Php

Heh, great idea. My thoughts, some contradictory...

There are precedents for civilizing bad languages by putting syntax preprocessors in front of them.

  • In the early days of Unix, Fortran was popular and about the only portable language because most machines had no C compiler. But the vanilla Fortran of the day didn't even have block structured if-then-else, just a goofy single-statement if or an if-goto. So, the Ratfor language was implemented as a preprocessor for Fortran-66.
  • I believe there were (are?) Cobol preprocessors that presumably dealt with the verbosity and limitations of early Cobol dialects.
  • To this day Unix-derived systems ship with a macro processor called m4.
  • Several CSS preprocessors are available today, most notably Sass and LESS.

But...

  • Just let it die, and the sooner the better

  • The problem isn't really in the syntax.

  • I don't see much of a JavaScript-PHP parallel. JavaScript is a great language. It's kind of the opposite of PHP.

  • I'm not sure why you say that PHP is a great language. It's one of the worst. Every decent feature is a patch or repatch in a recent version.

  • As you noted, there is a fixed-up version of PHP already: it's called Ruby and, as a language, it's near-perfect. There is another fixed-up version called Python. The world would be better off in the long run if we support the better systems.

Solution 4 - Php

It is here now. A new language which is to PHP what CoffeeScript is to Javascript. (I.e., awesome.)

SNOWSCRIPT


Snowscript code looks like this:


fn how_big_is_it(number)
    if number < 100
        <- "small"
    else
        <- "big"

PHP output looks like this:


function how_big_is_it($number) {
    if ($number < 100) {
        return "small";
    } else {
        return "big";
    }
}

All it needs now, is you.

Solution 5 - Php

If it would be to PHP what something like sass is to CSS, I'd be interested. But what would exactly would you want to add? Or would you just want to weed out the bad?

And what would you consider to be the bad?

Solution 6 - Php

Writing a PHP syntax transformer would probably be a neat project.

However, don't forget that PHP's standard library is a huge mess. Cleaning that up, would be a far bigger task.

Solution 7 - Php

The more I am thinking about this, the more irrealistic it sounds. The reason is simple: There actually are such language proprocessors already. Two of them (though not using PHP as implementation, only as compilation target) can be found here. But simply nobody uses them.

Yes, if the compiler itself were written in PHP, probably more people would use it. But I really can't see a way how to get this popular enough to be worth the work.

Another big problem is, that people mostly are used to their awesome code-highlighting, code-completing, code-inspecting IDE. Without getting IDE support probably merely anybody will use it (and IDE support can only be obtained by having many people use it...)

Thoughts?

Solution 8 - Php

I can see writing compilers to JavaScript (because the web imposes it upon us), but this sounds like a waste of time.

Haxe already does this, although it's not specifically targeted at PHP (linked to the Wikipedia article instead of their website because I'm afraid I'm going to get exploited if I visit the real site...)

> PHP is easy to deploy anywhere

...as are its vulnerabilities.

> I know it allows very bad design sometimes.

That's a bit of an understatement, it doesn't even have a module system, has no encapsulation, and has tons of silly things such as dynamic name resolution.

PHP is slow enough as it is, do you really want something an order of a magnitude slower?

Java is much more easy to deploy anyways, and lets you drop down to the bytecode level if you want. Java also gives you access to moderately sane libraries.

Solution 9 - Php

This is something I have thought about already often. PHP just is messy at some points.

Actually, I already have a project PrePHP focusing on providing PHP 5.3 functionality to PHP 5.2. But it adds some minor language features, like func()[0]. I haven't developed this project for some time and it definitely isn't "clean", but it shows, that what you want is possible and actually even not that complicated.

If you are serious about this, I am perfectly willing to collaborate with you.

Solution 10 - Php

Very interesting idea and if it come to life i think that i wan't to be involved in :)

For start You may check and read this position http://www.amazon.com/Masterminds-Programming-Conversations-Creators-Languages/dp/0596515170 (iam reading it now). It makes clear how really complicated is to maintain own language.

Solution 11 - Php

I agree that PHP definitely could do with some improvement, right now it allows for too much fooling around.

Some things I'd like to see

  • Static Typing
  • Required indentation
  • Proper use of objects (using arrays as objects is just stupid)

Then again, maybe I should just drop PHP and start working with Ruby or Python.

Solution 12 - Php

I'm like 8 years too late, but I'll answer anyways for anyone else who stumbles upon this.

Hack is a language developed by Facebook to deal with some of the issues of PHP, since Facebook had a large PHP codebase. Hack adds some nice features on top of PHP such as gradual typing (what TypeScript has) and generics, among other features, and gets rid of some of the more dangerous PHP features. Hack was at one point a superset of PHP, but is no longer completely compatible after removing some of the worse PHP features.

This is slightly different from what you were asking, since at this point Hack has its own interpreter, written by Facebook, but this started out as "better language that compiles to PHP", so I thought it was worth mentioning here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEber Freitas DiasView Question on Stackoverflow
Solution 1 - PhppolarblauView Answer on Stackoverflow
Solution 2 - PhpAndrew SkirrowView Answer on Stackoverflow
Solution 3 - PhpDigitalRossView Answer on Stackoverflow
Solution 4 - PhpProf. FalkenView Answer on Stackoverflow
Solution 5 - PhpsevenseacatView Answer on Stackoverflow
Solution 6 - PhpFelix GeisendörferView Answer on Stackoverflow
Solution 7 - PhpNikiCView Answer on Stackoverflow
Solution 8 - PhpL̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳View Answer on Stackoverflow
Solution 9 - PhpNikiCView Answer on Stackoverflow
Solution 10 - PhpTomaszSobczakView Answer on Stackoverflow
Solution 11 - PhpNaatanView Answer on Stackoverflow
Solution 12 - PhpCoffeeTableEspressoView Answer on Stackoverflow