Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Last active November 30, 2019 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/9cade86f7b16b87114cead7965eeaccb to your computer and use it in GitHub Desktop.
Save cosminpopescu14/9cade86f7b16b87114cead7965eeaccb to your computer and use it in GitHub Desktop.
Demo php 7.4
<?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;
}
}
<?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