Created
December 10, 2020 13:54
-
-
Save frv-dev/dede006b91d9687f6ba4528cdad45b8c to your computer and use it in GitHub Desktop.
Exemplos PHP 8 - Code Easy - DO NOT DELETE
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 | |
#[Attribute] | |
class Letter | |
{ | |
public const UPPER_CASE = 'UPPER_CASE'; | |
public const LOWER_CASE = 'LOWER_CASE'; | |
public const FIRST_CHARACTER_UPPER_CASE = 'FIRST_CASE_UPPER'; | |
public function __construct( | |
public string $modifier | |
) {} | |
public static function action(object $object): void | |
{ | |
$reflectionClass = new reflectionClass($object::class); | |
$properties = $reflectionClass->getProperties(); | |
foreach ($properties as $property) { | |
$property->setAccessible(true); | |
$attributes = $property->getAttributes(self::class); | |
foreach ($attributes as $attribute) { | |
$modifier = $attribute->newInstance()->modifier; | |
$value = $property->getValue($object); | |
$newValue = match ($modifier) { | |
self::UPPER_CASE => strtoupper($value), | |
self::LOWER_CASE => strtolower($value), | |
self::FIRST_CHARACTER_UPPER_CASE => ucfirst(strtolower($value)), | |
default => $value, | |
}; | |
$property->setValue($object, $newValue); | |
} | |
} | |
} | |
} | |
class Person | |
{ | |
#[Letter(Letter::LOWER_CASE)] | |
private string $name; | |
public function setName(string $name): self | |
{ | |
$this->name = $name; | |
Letter::action($this); | |
return $this; | |
} | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
} | |
$person1 = new Person(); | |
$person1->setName('FeLiPe'); | |
var_dump($person1); |
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 IVehicle | |
{ | |
public function move(): string; | |
} | |
class Car implements IVehicle | |
{ | |
public function move(): string | |
{ | |
return "The car is moving...\n"; | |
} | |
} | |
class Motorcycle implements IVehicle | |
{ | |
public function move(): string | |
{ | |
return "The motorcycle is moving...\n"; | |
} | |
} | |
// ============================ | |
// BEFORE | |
class Before | |
{ | |
private IVehicle $vehicle; | |
public function __construct(IVehicle $vehicle) | |
{ | |
$this->vehicle = $vehicle; | |
} | |
public function execute(): void | |
{ | |
echo "Before: {$this->vehicle->move()}"; | |
} | |
} | |
(new Before(new Car()))->execute(); | |
(new Before(new Motorcycle()))->execute(); | |
// After | |
class After | |
{ | |
public function __construct(private IVehicle $vehicle) {} | |
public function execute(): void | |
{ | |
echo "After: {$this->vehicle->move()}"; | |
} | |
} | |
(new After(new Car()))->execute(); | |
(new After(new Motorcycle()))->execute(); |
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 | |
// switch | |
// igualdade == | |
echo 'Switch: '; | |
switch (8.0) { | |
case '8.0': | |
echo "Wrong\n"; | |
break; | |
case 8.0: | |
echo "Right\n"; | |
break; | |
default: | |
echo "Why are you here?\n"; | |
break; | |
} | |
// match | |
// identidade === | |
echo 'Match: ' . match (8.0) { | |
'8.0' => "Wrong\n", | |
8.0 => "Right\n", | |
default => "Why are you here?\n", | |
}; |
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 buildMessage(string $greeting = 'Hello', string $name = 'Code Easy'): string | |
{ | |
return "{$greeting} {$name}"; | |
} | |
// BEFORE | |
$before = buildMessage('Hello', 'Felipe'); | |
// AFTER | |
$after = buildMessage(name: 'Felipe'); | |
// ==================================== | |
echo $before; | |
echo "\n"; | |
echo $after; | |
echo "\n"; |
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 | |
declare(strict_types=1); | |
// Before | |
/** | |
* @param float|int ...$numbers | |
* @return float|int | |
*/ | |
function sumBefore(...$numbers) | |
{ | |
$result = 0; | |
foreach ($numbers as $number) { | |
$result += $number; | |
} | |
return $result; | |
} | |
var_dump(sumBefore(4.5, 5.5, 10)); | |
echo "\n"; | |
// After | |
function sumAfter(float|int ...$numbers): float|int | |
{ | |
$result = 0; | |
foreach ($numbers as $number) { | |
$result += $number; | |
} | |
return $result; | |
} | |
var_dump(sumAfter(4.5, 5.5, 10)); | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment