endel / js2php

JavaScript (ES6) to PHP source-to-source transpiler.
https://endel.github.io/js2php/
MIT License
333 stars 41 forks source link

error in class inheritance #10

Closed francescoagati closed 10 years ago

francescoagati commented 10 years ago

class inherited isn't write in php code

class ArticlePage extends Page {
}

ArticlePage.db = {
  Date:'Date'
}
<?php
class ArticlePage extends [object Object]
{

}
ArticlePage::db = array("Date" => 'Date');

[object Object] should be Page

endel commented 10 years ago

@francescoagati thanks again for reporting. :)

Static members will not work, since esprima-fb, which is the EcmaScript parser I'm using, doesn't support it yet. :(

Maybe you could use static methods for now:

class ArticlePage {
  static db() {
    return {Date:'Date'};
  }
}
var_dump(ArticlePage.db());
francescoagati commented 10 years ago

Yes but in PHP static properties can be modified outside the body class Il 25/ott/2014 21:44 "Endel Dreyer" notifications@github.com ha scritto:

@francescoagati thanks again for reporting. :)

Static members will not work, since esprima-fb, which is the EcmaScript parser I'm using, doesn't support it yet. :(

Maybe you could use static methods for now:

class ArticlePage { static db() { return {Date:'Date'}; }}var_dump(ArticlePage.db());

— Reply to this email directly or view it on GitHub.

francescoagati commented 10 years ago

http://stackoverflow.com/questions/693691/how-to-initialize-static-variables Il 25/ott/2014 21:53 "francesco agati" francescoagati1975@gmail.com ha scritto:

Yes but in PHP static properties can be modified outside the body class Il 25/ott/2014 21:44 "Endel Dreyer" notifications@github.com ha scritto:

@francescoagati thanks again for reporting. :)

Static members will not work, since esprima-fb, which is the EcmaScript parser I'm using, doesn't support it yet. :(

Maybe you could use static methods for now:

class ArticlePage { static db() { return {Date:'Date'}; }}var_dump(ArticlePage.db());

— Reply to this email directly or view it on GitHub.

endel commented 10 years ago

@francescoagati the problem here is that it isn't possible to declare the static member, due EcmaScript parser limitation.

Without the variable declaration, PHP will not be able to modify it.

The following code (which is valid in ES6) isn't recognized by esprima-fb:

class Monster {
  // "static" places the property on the constructor.
  static allMonsters = {};
}

It results in parse error:

node_modules/esprima-fb/esprima.js:6718
            throw e;
                  ^
Error: Line 3: Unexpected token =

I've opened an issue for them here: https://github.com/facebook/esprima/issues/63

francescoagati commented 10 years ago

yes true i have see now. Static properties must be defined in body class. http://stackoverflow.com/a/9843883

but you can use the syntax supported from esprima to add feature to class body

for example for add static variables in body you could use this method

class ClassExample {

  __static__() {
    return {
      DB_FIELDS:{}
    }
  }
}

in compile time you can check the esistence of node function called static inside class body and use this for generate static properties. And after that you cancel the node function static