-
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.
Rebase Joe's anon classes implementation
- Loading branch information
Showing
19 changed files
with
438 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
declare bare anonymous class | ||
--FILE-- | ||
<?php | ||
var_dump(new class{}); | ||
--EXPECTF-- | ||
object(class@%s)#%d (0) { | ||
} | ||
|
||
|
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,21 @@ | ||
--TEST-- | ||
declare anonymous class extending another | ||
--FILE-- | ||
<?php | ||
class A{} | ||
|
||
interface B{ | ||
public function method(); | ||
} | ||
|
||
$a = new class extends A implements B { | ||
public function method(){ | ||
return true; | ||
} | ||
}; | ||
|
||
var_dump($a instanceof A, $a instanceof B); | ||
--EXPECTF-- | ||
bool(true) | ||
bool(true) | ||
|
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,54 @@ | ||
--TEST-- | ||
reusing anonymous classes | ||
--FILE-- | ||
<?php | ||
while (@$i++<10) { | ||
var_dump(new class($i) { | ||
|
||
public function __construct($i) { | ||
$this->i = $i; | ||
} | ||
}); | ||
} | ||
--EXPECTF-- | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(1) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(2) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(3) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(4) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(5) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(6) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(7) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(8) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(9) | ||
} | ||
object(class@%s)#1 (1) { | ||
["i"]=> | ||
int(10) | ||
} | ||
|
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,30 @@ | ||
--TEST-- | ||
testing anonymous inheritance | ||
--FILE-- | ||
<?php | ||
class Outer { | ||
protected $data; | ||
|
||
public function __construct($data) { | ||
$this->data = $data; | ||
} | ||
|
||
public function getArrayAccess() { | ||
/* create a proxy object implementing array access */ | ||
return new class($this->data) extends Outer implements ArrayAccess { | ||
public function offsetGet($offset) { return $this->data[$offset]; } | ||
public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); } | ||
public function offsetUnset($offset) { unset($this->data[$offset]); } | ||
public function offsetExists($offset) { return isset($this->data[$offset]); } | ||
}; | ||
} | ||
} | ||
|
||
$outer = new Outer(array( | ||
rand(1, 100) | ||
)); | ||
|
||
/* not null because inheritance */ | ||
var_dump($outer->getArrayAccess()[0]); | ||
--EXPECTF-- | ||
int(%d) |
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,36 @@ | ||
--TEST-- | ||
testing reusing anons that implement an interface | ||
--FILE-- | ||
<?php | ||
class Outer { | ||
protected $data; | ||
|
||
public function __construct(&$data) { | ||
/* array access will be implemented by the time we get to here */ | ||
$this->data = &$data; | ||
} | ||
|
||
public function getArrayAccess() { | ||
/* create a child object implementing array access */ | ||
/* this grants you access to protected methods and members */ | ||
return new class($this->data) implements ArrayAccess { | ||
public function offsetGet($offset) { return $this->data[$offset]; } | ||
public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); } | ||
public function offsetUnset($offset) { unset($this->data[$offset]); } | ||
public function offsetExists($offset) { return isset($this->data[$offset]); } | ||
}; | ||
} | ||
} | ||
|
||
$data = array( | ||
rand(1, 100), | ||
rand(2, 200) | ||
); | ||
|
||
$outer = new Outer($data); | ||
$proxy = $outer->getArrayAccess(); | ||
|
||
/* null because no inheritance, so no access to protected member */ | ||
var_dump(@$outer->getArrayAccess()[0]); | ||
--EXPECT-- | ||
NULL |
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,15 @@ | ||
--TEST-- | ||
testing anon classes inside namespaces | ||
--FILE-- | ||
<?php | ||
namespace lone { | ||
$hello = new class{} ; | ||
} | ||
|
||
namespace { | ||
var_dump ($hello); | ||
} | ||
--EXPECTF-- | ||
object(lone\class@%s)#1 (0) { | ||
} | ||
|
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,23 @@ | ||
--TEST-- | ||
testing anon classes in functions outside of classes in namespaces | ||
--FILE-- | ||
<?php | ||
namespace lone { | ||
function my_factory() { | ||
return new class{}; | ||
} | ||
|
||
class Outer { | ||
|
||
public function __construct() { | ||
var_dump( | ||
my_factory()); | ||
} | ||
} | ||
|
||
new Outer(); | ||
} | ||
--EXPECTF-- | ||
object(lone\class@%s)#2 (0) { | ||
} | ||
|
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,23 @@ | ||
--TEST-- | ||
testing static access for methods and properties in anon classes | ||
--FILE-- | ||
<?php | ||
$anonClass = new class("cats", "dogs") { | ||
public static $foo; | ||
private static $bar; | ||
|
||
public function __construct($foo, $bar) { | ||
static::$foo = $foo; | ||
static::$bar = $bar; | ||
} | ||
|
||
public static function getBar() { | ||
return static::$bar; | ||
} | ||
}; | ||
|
||
var_dump($anonClass::$foo); | ||
var_dump($anonClass::getBar()); | ||
--EXPECT-- | ||
string(4) "cats" | ||
string(4) "dogs" |
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,18 @@ | ||
--TEST-- | ||
testing traits in anon classes | ||
--FILE-- | ||
<?php | ||
|
||
trait Foo { | ||
public function someMethod() { | ||
return "bar"; | ||
} | ||
} | ||
|
||
$anonClass = new class { | ||
use Foo; | ||
}; | ||
|
||
var_dump($anonClass->someMethod()); | ||
--EXPECT-- | ||
string(3) "bar" |
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
Oops, something went wrong.