Skip to content

Commit 2e3be43

Browse files
committed
enh(php) add PHP constants
1 parent 015a00c commit 2e3be43

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/languages/php.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ export default function(hljs) {
380380
},
381381
VARIABLE,
382382
FUNCTION_INVOKE,
383+
{
384+
match: [
385+
/const/,
386+
regex.concat(WHITESPACE, "+"),
387+
IDENT_RE,
388+
regex.concat(WHITESPACE, "*="),
389+
],
390+
scope: {
391+
1: "keyword",
392+
3: "variable",
393+
},
394+
},
383395
{
384396
// swallow composed identifiers to avoid parsing them as keywords
385397
match: regex.concat(

test/markup/php/titles.expect.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> </span>{
2+
<span class="hljs-keyword">const</span> <span class="hljs-variable">FOO</span>=<span class="hljs-string">&#x27;foo&#x27;</span>;
3+
4+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params">
5+
<span class="hljs-keyword">public</span> <span class="hljs-keyword">readonly</span> <span class="hljs-keyword">string</span> <span class="hljs-variable">$name</span> = <span class="hljs-built_in">self</span>::<span class="hljs-variable">FOO</span>
6+
</span>) </span>{}
7+
8+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClass</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
9+
<span class="hljs-keyword">return</span> <span class="hljs-title class_">DateTimeImmutable</span>::<span class="hljs-variable language_">class</span>;
10+
}
11+
12+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClassFromSelf</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
13+
<span class="hljs-keyword">return</span> <span class="hljs-built_in">self</span>::<span class="hljs-variable language_">class</span>;
14+
}
15+
16+
<span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClassFromStatic</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
17+
<span class="hljs-keyword">return</span> <span class="hljs-built_in">static</span>::<span class="hljs-variable language_">class</span>;
18+
}
19+
}
20+
21+
<span class="hljs-variable">$date</span> = DateTimeImmutable::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>());
22+
<span class="hljs-keyword">echo</span> <span class="hljs-variable">$date</span>::<span class="hljs-variable language_">class</span>;

test/markup/php/titles.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
final class Example {
2+
const FOO='foo';
3+
4+
public function __construct(
5+
public readonly string $name = self::FOO
6+
) {}
7+
8+
public function getClass(): string {
9+
return DateTimeImmutable::class;
10+
}
11+
12+
public function getClassFromSelf(): string {
13+
return self::class;
14+
}
15+
16+
public static function getClassFromStatic(): string {
17+
return static::class;
18+
}
19+
}
20+
21+
$date = DateTimeImmutable::createFromMutable(new \DateTime());
22+
echo $date::class;

0 commit comments

Comments
 (0)