Skip to content

Instantly share code, notes, and snippets.

@frv-dev
Created December 10, 2020 13:54
Show Gist options
  • Save frv-dev/dede006b91d9687f6ba4528cdad45b8c to your computer and use it in GitHub Desktop.
Save frv-dev/dede006b91d9687f6ba4528cdad45b8c to your computer and use it in GitHub Desktop.
Exemplos PHP 8 - Code Easy - DO NOT DELETE
<?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);
<?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();
<?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",
};
<?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";
<?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