-1

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?

Alex
  • 715
  • 5
  • 14

1 Answers1

0

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.

Community
  • 1
  • 1
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • Is it possible to add a keyword to php? so, how the Zend company does it? – Alex Oct 22 '14 at 13:35
  • VS PHP can intellisence the code in future? – Alex Oct 22 '14 at 13:36
  • new kewords to php comes from at least two ways: source code of PHP, and extensions (DLLs in windows case) – Saic Siquot Oct 22 '14 at 13:50
  • A compiler does *not* work by simply `str_replacing` strings. That'd be an extremely primitive and limited compiler. – deceze Oct 22 '14 at 13:55
  • Yes, thats why its debatable if you can call it compiler, but it compiles to PHP code. and yes i would use regexes, but in the end it is just a search and replace (maybe setup in an OO environment). – Joel Harkes Oct 22 '14 at 13:57