Search code examples
phpphp-cpp

Adding a syntax for extending php rather than a library with php-cpp


I was interested in adding some feature to the php syntax, and definitly it's obvious for me it's different from writing modules for the language. I was wondering this requires to edit the resource in C which is avaialble on the php.net website and naturally C programming knowledge. Simply I want do the below:

xforeach ($array as $item with $counter) {
    echo "This is $counter time this loop is happening";
}

Rather than writing:

$counter = 0;
foreach ($array as $item) {
    echo "This is $counter time this loop is happening";
    $counter ++;
}

or in another glimpse, adding attribute to each class or function, like it's possible in C#

class MyClass {

    [Description("This is my php 6 version")]
    public function Test() {


    }
}

What is the first step on the way to achieving this approach?


Solution

  • See how other people did it: http://hacklang.org/ (from facebook)

    They build their own language on php (which compiles to php in the end)

    What you do is:

    1. you define your own file extension
    2. str_replace your own code with php code <= this will be your compiler.
    3. run your created php code.

    Note: this is also how template engines work, like Twig.

    discouragement

    In the end you either write php or you write C#.

    • Php doesnt need generics because it is not typechecked!

    Note: Even if you would succeed their would not be an IDE with IntelliSense to write your code in.

    why would you want to make php like C#, why not just code in C#?

    Note: vNext (MVC6) will be able to run on Linux servers as well, with the new Roslyn compiler.