Last active
November 30, 2019 07:59
-
-
Save cosminpopescu14/9cade86f7b16b87114cead7965eeaccb to your computer and use it in GitHub Desktop.
Demo php 7.4
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 User { | |
public int $id; | |
public string $name; | |
/** | |
* @return int | |
*/ | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
/** | |
* @param int $id | |
* @return User | |
*/ | |
public function setId(int $id): User | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
* @return User | |
*/ | |
public function setName(string $name): User | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
} |
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 'Demo.php'; | |
echo "Arrow function - array_map" . PHP_EOL; | |
$factor = 10; | |
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]); | |
print_r($nums); | |
echo "--------------------------" . PHP_EOL; | |
echo "Unpacking inside arrays aka spread operator" . PHP_EOL; | |
$parts = ['apple', 'pear']; | |
$fruits = ['banana', 'orange', ...$parts, 'watermelon']; | |
print_r($fruits); | |
echo "--------------------------" . PHP_EOL; | |
$demo = new User(); | |
$demo->setId("1") | |
->setName("Cosmin"); | |
echo $demo->getName(); | |
echo PHP_EOL; | |
echo "Arrow function - array_filter" . PHP_EOL; | |
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
$odds = array_filter($numbers, fn($n) => $n % 2 == 0); | |
print_r($odds); | |
echo "Expression" . PHP_EOL; | |
$mul2 = fn($x) => $x * 2; | |
echo $mul2(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment