-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Functional interfaces #1866
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
Closed
Functional interfaces #1866
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
--TEST-- | ||
functional interfaces: basic test | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(); | ||
} | ||
|
||
$foo = function () implements IFoo {}; | ||
|
||
var_dump($foo instanceof IFoo, | ||
$foo instanceof Closure); | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
functional interfaces: verify fail | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(string $arg) : void; | ||
} | ||
|
||
$foo = function () implements IFoo {}; | ||
--EXPECTF-- | ||
Fatal error: Declaration of {IFoo}::method() must be compatible with IFoo::method(string $arg): void in %s on line 6 | ||
|
This file contains hidden or 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,16 @@ | ||
--TEST-- | ||
functional interfaces: verify pass | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(string $arg) : void; | ||
} | ||
|
||
$foo = function (string $arg) implements IFoo : void {}; | ||
|
||
var_dump($foo instanceof IFoo, | ||
$foo instanceof Closure); | ||
--EXPECTF-- | ||
bool(true) | ||
bool(true) | ||
|
This file contains hidden or 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,15 @@ | ||
--TEST-- | ||
functional interfaces: not functional interface | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(string $arg) : void; | ||
public function other(); | ||
} | ||
|
||
$foo = function (string $arg) implements IFoo : void {}; | ||
--EXPECTF-- | ||
Fatal error: cannot implement non functional interface IFoo in %s on line 7 | ||
|
||
|
||
|
This file contains hidden or 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,14 @@ | ||
--TEST-- | ||
functional interfaces: non interface | ||
--FILE-- | ||
<?php | ||
class IFoo { | ||
public function method(string $arg) : void {} | ||
} | ||
|
||
$foo = function (string $arg) implements IFoo : void {}; | ||
--EXPECTF-- | ||
Fatal error: cannot implement non interface IFoo in %s on line 6 | ||
|
||
|
||
|
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
functional interfaces: interface call | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(string $arg) : void; | ||
} | ||
|
||
$foo = function (string $arg) implements IFoo : void { | ||
var_dump($arg); | ||
}; | ||
|
||
$foo->method("turtles ?"); | ||
--EXPECTF-- | ||
string(9) "turtles ?" | ||
|
||
|
||
|
||
|
This file contains hidden or 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,7 @@ | ||
--TEST-- | ||
functional interfaces: cannot implement self | ||
--FILE-- | ||
<?php | ||
function () implements self {}; | ||
--EXPECTF-- | ||
Fatal error: functional interface cannot implement self in %s on line 2 |
This file contains hidden or 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,7 @@ | ||
--TEST-- | ||
functional interfaces: cannot implement parent | ||
--FILE-- | ||
<?php | ||
function () implements parent {}; | ||
--EXPECTF-- | ||
Fatal error: functional interface cannot implement parent in %s on line 2 |
This file contains hidden or 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,7 @@ | ||
--TEST-- | ||
functional interfaces: cannot implement static | ||
--FILE-- | ||
<?php | ||
function () implements static {}; | ||
--EXPECTF-- | ||
Fatal error: functional interface cannot implement static in %s on line 2 |
This file contains hidden or 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,30 @@ | ||
--TEST-- | ||
functional interfaces: scope/this | ||
--FILE-- | ||
<?php | ||
interface ILog { | ||
public function log(string $message, ...$args); | ||
} | ||
|
||
class Foo { | ||
|
||
public function __construct() { | ||
$this->stream = fopen("php://stdout", "w"); | ||
} | ||
|
||
public function getLogger() : ILog { | ||
return function (string $message, ... $args) implements ILog { | ||
fprintf($this->stream, $message, ... $args); | ||
}; | ||
} | ||
|
||
protected $stream; | ||
} | ||
|
||
$foo = new Foo(); | ||
$logger = | ||
$foo->getLogger(); | ||
$logger->log("php7 baby\n"); | ||
?> | ||
--EXPECTF-- | ||
php7 baby |
This file contains hidden or 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,42 @@ | ||
--TEST-- | ||
functional interfaces: call method handling | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
private $bar = []; | ||
|
||
public function __construct(array $bar) { | ||
$this->bar = $bar; | ||
} | ||
|
||
public function getShufflingIterator() : Traversable { | ||
|
||
return function() implements IteratorAggregate : Traversable { | ||
shuffle($this->bar); | ||
|
||
return new ArrayIterator($this->bar); | ||
}; | ||
} | ||
} | ||
|
||
$foo = new Foo([ | ||
"hello", | ||
"world" | ||
]); | ||
|
||
$shuffler = | ||
$foo->getShufflingIterator(); | ||
|
||
foreach ($shuffler as $key => $value) { | ||
var_dump($value); | ||
} | ||
|
||
foreach ($shuffler as $key => $value) { | ||
var_dump($value); | ||
} | ||
?> | ||
--EXPECTF-- | ||
string(5) "%s" | ||
string(5) "%s" | ||
string(5) "%s" | ||
string(5) "%s" |
This file contains hidden or 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,25 @@ | ||
--TEST-- | ||
functional interfaces: binding | ||
--FILE-- | ||
<?php | ||
interface ILog { | ||
public function do() : void; | ||
} | ||
|
||
class Foo {} | ||
|
||
$logger = function() implements ILog : void { | ||
var_dump($this instanceof Foo); | ||
}; | ||
|
||
$foo = new Foo(); | ||
|
||
$bound = Closure::bind($logger, $foo); | ||
|
||
var_dump($bound instanceof ILog); | ||
|
||
$bound(); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) |
This file contains hidden or 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,70 @@ | ||
--TEST-- | ||
functional interfaces: call method without ce and proxy | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
private $bar = []; | ||
|
||
public function fill($limit = 100) { | ||
for ($i = 0; $i < $limit; $i++) { | ||
$this->bar[] = mt_rand($i, $limit); | ||
} | ||
} | ||
|
||
public function getEvenCounter() : Countable { | ||
return function () implements Countable { | ||
$counter = 0; | ||
foreach ($this->bar as $value) { | ||
if ($value % 2 === 0) | ||
$counter++; | ||
} | ||
return $counter; | ||
}; | ||
} | ||
|
||
public function getOddCounter() : Countable { | ||
return function () implements Countable { | ||
$counter = 0; | ||
foreach ($this->bar as $value) { | ||
if ($value % 2 !== 0) { | ||
$counter++; | ||
} | ||
} | ||
return $counter; | ||
}; | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
$even = $foo->getEvenCounter(); | ||
$odd = $foo->getOddCounter(); | ||
|
||
$it = 0; | ||
|
||
while (++$it<10) { | ||
$foo->fill(50); | ||
var_dump( | ||
count($even), | ||
count($odd)); | ||
} | ||
?> | ||
--EXPECTF-- | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) | ||
int(%d) |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
functional interfaces: non static implementation of static interface | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public static function method(); | ||
} | ||
|
||
$cb = function () implements IFoo {}; | ||
--EXPECTF-- | ||
Fatal error: cannot create non static implementation of static functional interface IFoo in %s on line 6 | ||
|
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
functional interfaces: static implementation of non static interface | ||
--FILE-- | ||
<?php | ||
interface IFoo { | ||
public function method(); | ||
} | ||
|
||
$cb = static function () implements IFoo {}; | ||
--EXPECTF-- | ||
Fatal error: cannot create static implementation of non static functional interface IFoo in %s on line 6 | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐢!!