node: emit static ::class as class name string literal

This commit is contained in:
mappu 2020-04-11 12:44:37 +12:00
parent 3fba39b562
commit d3d3d34116
2 changed files with 24 additions and 1 deletions

View File

@ -17,7 +17,8 @@ function Foo() {
class Bar {
function Baz() {
echo __CLASS__ . "::" . __METHOD__ ;
echo Baz::class;
echo Bar::class;
// echo $this::class;
}
}

22
node.go
View File

@ -1012,6 +1012,28 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
constName := n.ConstantName.(*node.Identifier).Value
// TODO fix up visibility modifier
// Special case: `::class` is just the string name of the class
if constName == `class` {
// This can be known statically (e.g. MyClass::class --> "MyClass") but
// isn't generally known non-statically
if _, ok := n.Class.(*name.Name); ok {
// Static
return strconv.Quote(className), nil
} else if className == `self` {
return strconv.Quote(this.currentClassName), nil
} else {
// Dynamic
// Translate to reflect
// this.importPackages["reflect"] = struct{}{}
// return `reflect.TypeOf(` + className + `).String()`, nil
// Actually PHP doesn't support using ::class on variables
return "", parseErr{n, fmt.Errorf(`PHP Fatal error: Dynamic class names are not allowed in compile-time ::class fetch`)}
}
}
return className + constName, nil
case *expr.Exit: