Skip to content
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

Checking that Closure is static #7193

Closed
wants to merge 1 commit 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
71 changes: 71 additions & 0 deletions Zend/tests/closure_063.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
Closure 063: Checking that closure is static
--FILE--
<?php

$staticUnscoped = static function () {};
$nonstaticUnscoped = function () {};

class A {
function getStaticClosure() {
return static function () {};
}
function getNonStaticClosure() {
return function () {};
}
}

$a = new A();
$staticScoped = $a->getStaticClosure();
$nonstaticScoped = $a->getNonStaticClosure();

function function_callable() {}

$nonstaticFromFunction = Closure::fromCallable('function_callable');

class B {
function nonStaticMethod() {}
static function staticMethod() {}
}

$staticFromMethod = Closure::fromCallable([B::class, 'staticMethod']);
$nonstaticFromMethod = Closure::fromCallable([new B(), 'nonStaticMethod']);

echo "Unscoped Closure\n";
var_dump($staticUnscoped->isStatic());
var_dump($nonstaticUnscoped->isStatic());
echo "\n";

echo "Scoped Closure\n";
var_dump($staticScoped->isStatic());
var_dump($nonstaticScoped->isStatic());
echo "\n";

echo "From Callable Function\n";
var_dump($nonstaticFromFunction->isStatic());
echo "\n";

echo "From Callable Method\n";
var_dump($staticFromMethod->isStatic());
var_dump($nonstaticFromMethod->isStatic());
echo "\n";

echo "Done\n";
?>
--EXPECTF--
Unscoped Closure
bool(true)
bool(false)

Scoped Closure
bool(true)
bool(false)

From Callable Function
bool(false)

From Callable Method
bool(true)
bool(false)

Done
11 changes: 11 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ ZEND_METHOD(Closure, call)
}
/* }}} */

ZEND_METHOD(Closure, isStatic) /* {{{ */
{
ZEND_PARSE_PARAMETERS_NONE();

zend_closure *closure = (zend_closure *) Z_OBJ_P(ZEND_THIS);
zend_function *func = &closure->func;

RETURN_BOOL(func->common.fn_flags & ZEND_ACC_STATIC);
}
/* }}} */

static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str)
{
zend_class_entry *ce, *called_scope;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_closures.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static function bind(
object|string|null $newScope = "static"
): ?Closure {}

public function isStatic(): bool {}

public function bindTo(?object $newThis, object|string|null $newScope = "static"): ?Closure {}

public function call(object $newThis, mixed ...$args): mixed {}
Expand Down
7 changes: 6 additions & 1 deletion Zend/zend_closures_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 7c4df531cdb30ac4206f43f0d40098666466b9a6 */
* Stub hash: 1e2c3090490b59afcf468d485fd44ef7d005bf41 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Closure___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
Expand All @@ -10,6 +10,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bind, 0, 2, Closure
ZEND_ARG_TYPE_MASK(0, newScope, MAY_BE_OBJECT|MAY_BE_STRING|MAY_BE_NULL, "\"static\"")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Closure_isStatic, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Closure_bindTo, 0, 1, Closure, 1)
ZEND_ARG_TYPE_INFO(0, newThis, IS_OBJECT, 1)
ZEND_ARG_TYPE_MASK(0, newScope, MAY_BE_OBJECT|MAY_BE_STRING|MAY_BE_NULL, "\"static\"")
Expand All @@ -27,6 +30,7 @@ ZEND_END_ARG_INFO()

ZEND_METHOD(Closure, __construct);
ZEND_METHOD(Closure, bind);
ZEND_METHOD(Closure, isStatic);
ZEND_METHOD(Closure, bindTo);
ZEND_METHOD(Closure, call);
ZEND_METHOD(Closure, fromCallable);
Expand All @@ -35,6 +39,7 @@ ZEND_METHOD(Closure, fromCallable);
static const zend_function_entry class_Closure_methods[] = {
ZEND_ME(Closure, __construct, arginfo_class_Closure___construct, ZEND_ACC_PRIVATE)
ZEND_ME(Closure, bind, arginfo_class_Closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(Closure, isStatic, arginfo_class_Closure_isStatic, ZEND_ACC_PUBLIC)
ZEND_ME(Closure, bindTo, arginfo_class_Closure_bindTo, ZEND_ACC_PUBLIC)
ZEND_ME(Closure, call, arginfo_class_Closure_call, ZEND_ACC_PUBLIC)
ZEND_ME(Closure, fromCallable, arginfo_class_Closure_fromCallable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
Expand Down