-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax closure $this unbinding deprecation
Only deprecate unbinding of $this from a closure if $this is syntactically used within the closure. This is desired to support Laravel's macro system, see laravel/framework#29482. This should still allow us to implement the performance improvements we're interested in for PHP 8, without breaking existing use-cases.
- Loading branch information
Showing
5 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--TEST-- | ||
Closure $this unbinding deprecation | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public function method() { | ||
echo "instance scoped, non-static, \$this used\n"; | ||
$fn = function() { | ||
var_dump($this); | ||
}; | ||
$fn->bindTo(null); | ||
echo "instance scoped, static, \$this used\n"; | ||
$fn = static function() { | ||
var_dump($this); | ||
}; | ||
$fn->bindTo(null); | ||
echo "instance scoped, non-static, \$this not used\n"; | ||
$fn = function() { | ||
var_dump($notThis); | ||
}; | ||
$fn->bindTo(null); | ||
} | ||
|
||
public static function staticMethod() { | ||
echo "static scoped, non-static, \$this used\n"; | ||
$fn = function() { | ||
var_dump($this); | ||
}; | ||
$fn->bindTo(null); | ||
echo "static scoped, static, \$this used\n"; | ||
$fn = static function() { | ||
var_dump($this); | ||
}; | ||
$fn->bindTo(null); | ||
echo "static scoped, static, \$this not used\n"; | ||
$fn = function() { | ||
var_dump($notThis); | ||
}; | ||
$fn->bindTo(null); | ||
} | ||
} | ||
|
||
(new Test)->method(); | ||
Test::staticMethod(); | ||
|
||
?> | ||
--EXPECTF-- | ||
instance scoped, non-static, $this used | ||
|
||
Deprecated: Unbinding $this of closure is deprecated in %s on line %d | ||
instance scoped, static, $this used | ||
instance scoped, non-static, $this not used | ||
static scoped, non-static, $this used | ||
static scoped, static, $this used | ||
static scoped, static, $this not used |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,8 @@ static zend_bool zend_valid_closure_binding( | |
} else { | ||
zend_error(E_DEPRECATED, "Unbinding $this of a method is deprecated"); | ||
} | ||
} else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr)) { | ||
} else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr) | ||
&& (func->common.fn_flags & ZEND_ACC_USES_THIS)) { | ||
// TODO: Only deprecate if it had $this *originally*? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nikic
Author
Member
|
||
zend_error(E_DEPRECATED, "Unbinding $this of closure is deprecated"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@nikic This TODO comment could be removed, couldn't it?