parent
05a409ac37
commit
8a423d225e
5 changed files with 223 additions and 0 deletions
@ -0,0 +1,57 @@ |
||||
--TEST-- |
||||
abstract class and abstract method |
||||
--FILE-- |
||||
<?php |
||||
|
||||
abstract class Animal { |
||||
protected string $name; |
||||
|
||||
public function __construct(string $name) { |
||||
$this->name = $name; |
||||
} |
||||
|
||||
abstract public function speak(): string; |
||||
|
||||
public function getName(): string { |
||||
return $this->name; |
||||
} |
||||
|
||||
public function describe(): string { |
||||
return $this->getName() . " says " . $this->speak(); |
||||
} |
||||
} |
||||
|
||||
class Dog extends Animal { |
||||
public function speak(): string { |
||||
return "woof"; |
||||
} |
||||
} |
||||
|
||||
class Cat extends Animal { |
||||
public function speak(): string { |
||||
return "meow"; |
||||
} |
||||
} |
||||
|
||||
function main() { |
||||
$dog = new Dog("Buddy"); |
||||
$cat = new Cat("Kitty"); |
||||
|
||||
var_dump($dog->getName()); |
||||
var_dump($dog->speak()); |
||||
var_dump($dog->describe()); |
||||
|
||||
var_dump($cat->getName()); |
||||
var_dump($cat->speak()); |
||||
|
||||
echo "done\n"; |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
string(5) "Buddy" |
||||
string(4) "woof" |
||||
string(15) "Buddy says woof" |
||||
string(5) "Kitty" |
||||
string(4) "meow" |
||||
done |
||||
@ -0,0 +1,21 @@ |
||||
--TEST-- |
||||
shell_exec / backtick operator |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function main() { |
||||
$output = `echo hello`; |
||||
var_dump(trim($output)); |
||||
|
||||
$who = "world"; |
||||
$output2 = `echo $who`; |
||||
var_dump(trim($output2)); |
||||
|
||||
echo "done\n"; |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
string(5) "hello" |
||||
string(5) "world" |
||||
done |
||||
@ -0,0 +1,30 @@ |
||||
--TEST-- |
||||
eval() language construct |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function main() { |
||||
// eval with return value |
||||
$result = eval('return 10 + 5;'); |
||||
var_dump($result); |
||||
|
||||
// eval returning string |
||||
$msg = eval('return "hello world";'); |
||||
var_dump($msg); |
||||
|
||||
// eval with static expressions |
||||
$sum = 0; |
||||
for ($i = 1; $i <= 3; $i++) { |
||||
eval('$GLOBALS["__total"] = ($GLOBALS["__total"] ?? 0) + ' . $i . ';'); |
||||
} |
||||
var_dump($GLOBALS["__total"]); |
||||
|
||||
echo "done\n"; |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
int(15) |
||||
string(11) "hello world" |
||||
int(6) |
||||
done |
||||
@ -0,0 +1,50 @@ |
||||
--TEST-- |
||||
final class and final method |
||||
--FILE-- |
||||
<?php |
||||
|
||||
class Base { |
||||
public function overridable(): string { |
||||
return "base"; |
||||
} |
||||
|
||||
final public function locked(): string { |
||||
return "cannot override"; |
||||
} |
||||
} |
||||
|
||||
class Derived extends Base { |
||||
public function overridable(): string { |
||||
return "derived"; |
||||
} |
||||
} |
||||
|
||||
final class Sealed { |
||||
public function value(): int { |
||||
return 42; |
||||
} |
||||
} |
||||
|
||||
function main() { |
||||
$base = new Base(); |
||||
var_dump($base->overridable()); |
||||
var_dump($base->locked()); |
||||
|
||||
$derived = new Derived(); |
||||
var_dump($derived->overridable()); |
||||
var_dump($derived->locked()); |
||||
|
||||
$sealed = new Sealed(); |
||||
var_dump($sealed->value()); |
||||
|
||||
echo "done\n"; |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
string(4) "base" |
||||
string(15) "cannot override" |
||||
string(7) "derived" |
||||
string(15) "cannot override" |
||||
int(42) |
||||
done |
||||
@ -0,0 +1,65 @@ |
||||
--TEST-- |
||||
interface implementation |
||||
--FILE-- |
||||
<?php |
||||
|
||||
interface Logger { |
||||
public function log(string $message): void; |
||||
} |
||||
|
||||
interface Formatter { |
||||
public function format(string $message): string; |
||||
} |
||||
|
||||
class ConsoleLogger implements Logger { |
||||
public function log(string $message): void { |
||||
echo "LOG: " . $message . "\n"; |
||||
} |
||||
} |
||||
|
||||
class JsonLogger implements Logger, Formatter { |
||||
private array $entries = []; |
||||
|
||||
public function log(string $message): void { |
||||
$this->entries[] = $message; |
||||
} |
||||
|
||||
public function format(string $message): string { |
||||
return '{"msg":"' . $message . '"}'; |
||||
} |
||||
|
||||
public function getEntries(): array { |
||||
return $this->entries; |
||||
} |
||||
} |
||||
|
||||
function main() { |
||||
$console = new ConsoleLogger(); |
||||
$console->log("test message"); |
||||
|
||||
$json = new JsonLogger(); |
||||
$json->log("hello"); |
||||
$json->log("world"); |
||||
var_dump($json->getEntries()); |
||||
var_dump($json->format("hi")); |
||||
|
||||
$logger = $json; |
||||
var_dump($logger instanceof Logger); |
||||
var_dump($logger instanceof Formatter); |
||||
|
||||
echo "done\n"; |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
LOG: test message |
||||
array(2) { |
||||
[0]=> |
||||
string(5) "hello" |
||||
[1]=> |
||||
string(5) "world" |
||||
} |
||||
string(12) "{"msg":"hi"}" |
||||
bool(true) |
||||
bool(true) |
||||
done |
||||
Loading…
Reference in new issue