Issue
I was messing around with the new Attributes
(a.k.a. "Annotations") in PHP
and I have this annoying issue, that Netbeans 12.2
does not recognize my valid PHP 8.0
code. I have tested it on my local machine and also here: https://sandbox.onlinephpfunctions.com/.
The code seems to compile just fine though. Other PHP 8.0
functions do work in the IDE, too (... well, it's the same binary). Can I fix or rather mute this error somehow? Because it marks my whole workspace as erroneous.
<?php
#[Routing('hello')]
class A {
public function __construct() {}
}
#[Routing('world')]
class B {
public function __construct() {}
}
class Route {
public function __construct(public string $name) {}
}
#[Attribute(Attribute::TARGET_CLASS)]
class Routing {
private static $routes = [];
public function __construct(public string $name) {
static::add(new Route($name));
}
private function add($route) {
static::$routes[] = $route;
}
public static function getRoutes() {
return static::$routes;
}
}
$rA = new ReflectionClass(A::class);
$rB = new ReflectionClass(B::class);
$attributeA = $rA->getAttributes()[0];
$attributeB = $rB->getAttributes()[0];
$a = $attributeA->newInstance();
$b = $attributeB->newInstance();
print_r($a);
print_r($b);
print_r($a::getRoutes());
I guess this is a bug. I could use the old syntax, but this is just a workaround in case I cannot find a solution.
Question:
How can I get rid of this error message or is there something that I am missing in my code, that should be there?
Solution
Note to future readers: this answer was written on 15th Jan 2021
The best advice is probably "be patient": PHP 8.0.0 was released on 26th Nov 2020, and while NetBeans 12.2 (released on 7th December 2020) includes some PHP 8 support there's still an open issue tracking further changes.
In particular, support for Constructor Property Promotion is being worked on in this PR, which is currently labelled as targeting release 12.3, which should be complete in the second half of February 2021.
Until then, you'll either have to live with your IDE thinking your source is invalid, or switch to a different IDE (PhpStorm included the necessary changes in version 2020.3, released 3rd December 2020, if you can afford that).
As an aside, if anyone working on the PHP functionality of NetBeans sees this, you might want to update this marketing page which is the top hit for "netbeans php" and currently brags about its cutting edge support for PHP 5.6!
Answered By - IMSoP