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
<?php | |
class Person{ | |
private $surname = "Doe"; | |
function getSurname(callable $fn){ | |
return $fn($this->surname); | |
} | |
} | |
echo (new Person())->getSurname("strtoupper"); //DOE |
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
<?php | |
require_once "vendor/autoload.php"; | |
function printStatus(CharacterUndoDecorator $decorator) | |
{ | |
list('x' => $x,'y' => $y,'speed' => $speed) = $decorator->getXYAndSpeed(); | |
echo "Current Status: x:{$x}, y:{$y}, speed:{$speed}" . "<br> \n"; | |
} | |
function undoAndPrint(CharacterUndoDecorator $decorator) |
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
<?php | |
class CharacterUndoDecorator implements CharacterInterface | |
{ | |
private CharacterInterface $character; | |
private array $undoStack = []; | |
public function __construct(CharacterInterface $character) | |
{ |
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
<?php | |
class Human implements CharacterInterface | |
{ | |
private int $x,$y,$speed; | |
public function __construct(int $x = 0,int $y = 0,int $speed = 3) | |
{ | |
$this->x = $x; | |
$this->y = $y; |
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
<?php | |
class Bird implements CharacterInterface | |
{ | |
private int $x,$y,$speed; | |
public function __construct(int $x = 0,int $y = 0,int $speed = 5) | |
{ | |
$this->x = $x; |
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
<?php | |
interface CharacterInterface | |
{ | |
public function moveInXAxis(int $times): void; | |
public function moveInYAxis(int $times): void; | |
public function changeSpeed(int $change): void; |
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
<?php | |
$math = function(int ...$args) : array{ | |
$operations = []; | |
$operations["Addition"] = function() use ($args){ | |
$result = 0; | |
foreach($args as $val) | |
$result += $val; | |
return $result; |
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
<?php | |
function hello(){ | |
return function() { | |
echo "Hello"; | |
}; | |
} | |
echo get_class( hello() ); // Closure |
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
<?php | |
//It takes a function as an argument | |
function execute(callable $myFunc) | |
{ | |
$myFunc(); | |
} | |
//Prints "Hello there!" | |
execute(function(){ | |
echo "Hello there!"; |
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
<?php | |
function sum(int ...$args){ | |
$sumVal = 0; | |
for($i = 0;$i < count($args);$i++) | |
$sumVal += $args[$i]; | |
return $sumVal; | |
} |
NewerOlder