Skip to content

GH-15723: add ReflectionConstant::getFileName() #15847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ PHP 8.5 UPGRADE NOTES
statement from the DEALLOCATE sql command in that we can reuse
its name afterwards.

- Reflection:
. ReflectionConstant::getFileName() was introduced.

========================================
7. New Classes and Interfaces
========================================
Expand Down
20 changes: 20 additions & 0 deletions Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ void free_zend_constant(zval *zv)
if (c->name) {
zend_string_release_ex(c->name, 0);
}
if (c->filename) {
zend_string_release_ex(c->filename, 0);
}
efree(c);
} else {
zval_internal_ptr_dtor(&c->value);
if (c->name) {
zend_string_release_ex(c->name, 1);
}
if (c->filename) {
zend_string_release_ex(c->filename, 1);
}
free(c);
}
}
Expand All @@ -68,6 +74,9 @@ static void copy_zend_constant(zval *zv)

c = Z_PTR_P(zv);
c->name = zend_string_copy(c->name);
if (c->filename != NULL) {
c->filename = zend_string_copy(c->filename);
}
if (Z_TYPE(c->value) == IS_STRING) {
Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
}
Expand Down Expand Up @@ -495,13 +504,24 @@ ZEND_API zend_result zend_register_constant(zend_constant *c)
name = c->name;
}

zend_string *filename = zend_get_executed_filename_ex();
if (filename == NULL) {
c->filename = NULL;
} else {
c->filename = zend_string_copy(filename);
}

/* Check if the user is trying to define any special constant */
if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__")
|| (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name)))
|| zend_hash_add_constant(EG(zend_constants), name, c) == NULL
) {
zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name));
zend_string_release(c->name);
if (c->filename) {
zend_string_release(c->filename);
c->filename = NULL;
}
if (!persistent) {
zval_ptr_dtor_nogc(&c->value);
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
typedef struct _zend_constant {
zval value;
zend_string *name;
zend_string *filename;
} zend_constant;

#define ZEND_CONSTANT_FLAGS(c) \
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
if (c->name) {
zend_string_release_ex(c->name, 0);
}
if (c->filename) {
zend_string_release_ex(c->filename, 0);
}
efree(c);
zend_string_release_ex(key, 0);
} ZEND_HASH_MAP_FOREACH_END_DEL();
Expand Down
14 changes: 14 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -7835,6 +7835,20 @@ ZEND_METHOD(ReflectionConstant, isDeprecated)
RETURN_BOOL(ZEND_CONSTANT_FLAGS(const_) & CONST_DEPRECATED);
}

ZEND_METHOD(ReflectionConstant, getFileName)
{
reflection_object *intern;
zend_constant *const_;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(const_);
if (const_->filename != NULL) {
RETURN_STR_COPY(const_->filename);
}
RETURN_FALSE;
}

ZEND_METHOD(ReflectionConstant, __toString)
{
reflection_object *intern;
Expand Down
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,5 +914,7 @@ public function getValue(): mixed {}

public function isDeprecated(): bool {}

public function getFileName(): string|false {}

public function __toString(): string {}
}
7 changes: 6 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions ext/reflection/tests/ReflectionConstant_getFileName.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
ReflectionConstant::getFileName()
--FILE--
<?php

include "included5.inc";

function testConstant(string $name): void {
$ref = new ReflectionConstant($name);
echo "$name: ";
var_dump($ref->getFileName());
}

define('IN_CURRENT_FILE_DEFINED', 42);
const IN_CURRENT_FILE_AST = 123;

echo "From PHP:\n";
testConstant('PHP_VERSION');
testConstant('STDIN');
testConstant('STDOUT');
testConstant('STDERR');

echo "\nFrom the current file:\n";
testConstant('IN_CURRENT_FILE_DEFINED');
testConstant('IN_CURRENT_FILE_AST');

echo "\nFrom an included file:\n";
testConstant('INCLUDED_CONSTANT_DEFINED');
testConstant('INCLUDED_CONSTANT_AST');
?>
--EXPECTF--
From PHP:
PHP_VERSION: bool(false)
STDIN: bool(false)
STDOUT: bool(false)
STDERR: bool(false)

From the current file:
IN_CURRENT_FILE_DEFINED: string(%d) "%sReflectionConstant_getFileName.php"
IN_CURRENT_FILE_AST: string(%d) "%sReflectionConstant_getFileName.php"

From an included file:
INCLUDED_CONSTANT_DEFINED: string(%d) "%sincluded5.inc"
INCLUDED_CONSTANT_AST: string(%d) "%sincluded5.inc"
5 changes: 5 additions & 0 deletions ext/reflection/tests/included5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
define('INCLUDED_CONSTANT_DEFINED', 'Foo');

const INCLUDED_CONSTANT_AST = 'Bar';
?>
Loading