Skip to content

Commit 205bd8e

Browse files
committed
Add PhpCompilerAttribute and PhpAttribute classes.
1 parent 5d06c35 commit 205bd8e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
attributes: Add PhpCompilerAttribute
3+
--FILE--
4+
<?php
5+
6+
<<PhpCompilerAttribute>>
7+
class Foo
8+
{
9+
}
10+
11+
$ref = new ReflectionClass(Foo::class);
12+
var_dump($ref->getAttributes()[0]->getAsObject());
13+
--EXPECTF--
14+
object(PhpCompilerAttribute)#3 (0) {
15+
}

Zend/tests/attributes/rfcexample.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Attributes: RFC Example
3+
--FILE--
4+
<?php
5+
namespace My\Attributes {
6+
7+
<<PhpAttribute>>
8+
class SingleArgument {
9+
public $argumentValue;
10+
11+
public function __construct($argumentValue) {
12+
$this->argumentValue = $argumentValue;
13+
}
14+
}
15+
}
16+
17+
namespace {
18+
use My\Attributes\SingleArgument;
19+
20+
<<SingleArgument("Hello World")>>
21+
class Foo {
22+
}
23+
24+
$reflectionClass = new \ReflectionClass(Foo::class);
25+
$attributes = $reflectionClass->getAttributes();
26+
27+
var_dump($attributes[0]->getName());
28+
var_dump($attributes[0]->getArguments());
29+
var_dump($attributes[0]->getAsObject());
30+
}
31+
--EXPECTF--
32+
string(28) "My\Attributes\SingleArgument"
33+
array(1) {
34+
[0]=>
35+
string(11) "Hello World"
36+
}
37+
object(My\Attributes\SingleArgument)#3 (1) {
38+
["argumentValue"]=>
39+
string(11) "Hello World"
40+
}

Zend/zend_default_classes.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
#include "zend_generators.h"
2727
#include "zend_weakrefs.h"
2828

29+
zend_class_entry *zend_ce_php_attribute;
30+
zend_class_entry *zend_ce_php_compiler_attribute;
31+
32+
static void zend_register_attribute_ce(void)
33+
{
34+
zend_class_entry ce;
35+
36+
INIT_CLASS_ENTRY(ce, "PhpAttribute", NULL);
37+
zend_ce_php_attribute = zend_register_internal_class(&ce);
38+
zend_ce_php_attribute->ce_flags |= ZEND_ACC_FINAL;
39+
40+
//zend_hash_init_ex(zend_ce_php_attribute->attributes, 8, NULL, NULL, 1, 0);
41+
//zend_hash_str_add_empty_element(zend_ce_php_attribute->attributes, "phpattribute", sizeof("phpattribute")-1);
42+
43+
INIT_CLASS_ENTRY(ce, "PhpCompilerAttribute", NULL);
44+
zend_ce_php_compiler_attribute = zend_register_internal_class(&ce);
45+
zend_ce_php_compiler_attribute->ce_flags |= ZEND_ACC_FINAL;
46+
47+
//zend_hash_init_ex(zend_ce_php_attribute->attributes, 8, NULL, NULL, 1, 0);
48+
//zend_hash_str_add_empty_element(zend_ce_php_attribute->attributes, "phpattribute", sizeof("phpattribute")-1);
49+
}
50+
2951
ZEND_API void zend_register_default_classes(void)
3052
{
3153
zend_register_interfaces();
@@ -34,4 +56,5 @@ ZEND_API void zend_register_default_classes(void)
3456
zend_register_closure_ce();
3557
zend_register_generator_ce();
3658
zend_register_weakref_ce();
59+
zend_register_attribute_ce();
3760
}

0 commit comments

Comments
 (0)