<?php
declare(strict_types=1);
//echo phpinfo();
echo "<h1>PHP 7.2</h1>";
echo "ver get_class not null";
echo "<h3>PARSE STR</h3>";
$str = "title=php8power&hosters=Haroldo&class[a]=update&class[b]=features&obj[teste]=123";
parse_str($str, $rs);
$rs = (is_array($rs) && count($rs)) ? (object) $rs : false;
if ($rs) {
varDump(
$rs
);
}
echo "<h3>NUMBER FORMAT</h3>";
$amount = 1155;
echo number_format(floatval($amount), 2, ',', '.');
echo "<h3>TIPAGEM</h3>";
function user(object $user): object
{
return $user;
}
$user = new stdClass;
$user->name = 'Maria de Souza';
$user->email = 'mariadesouza@gmail.com';
varDump(
[
user($user),
user($user)->name,
]
);
echo "<h1>PHP 7.3</h1>";
echo "ver: json throw, array first, last key, EOT, is_contable";
echo "<h3>LIST</h3>";
$addr = ["Rua X", 255, "SBC", 'SP'];
list($street, $number, $city, $state, &$zipCode) = $addr;
$addr[4] = '09851395';
varDump([$street, $number, $city, $state, $zipCode]);
echo "<h3>COMPACT</h3>";
$name = 'Maria';
$email = 'maria@gmail.com';
$array = compact('name', 'email');
varDump($array);
echo "<h3>SETCOOKIE</h3>";
setcookie('cookieA', 'HANDCLASS', time() + 3600);
setcookie('cookieB', 'HANDCLASS', [
'expires' => time() + 3600,
'secure' => true,
]);
varDump($_COOKIE);
echo "<h1>PHP 7.4</h1>";
echo 'ver: reference type, array inside parts ';
echo "<h3>TYPE PROPERTIES</h3>";
class Addr
{
public string $fullAddr;
}
class UserNot
{
public $name;
public $age;
public $addr;
}
$userNote = new UserNot();
$userNote->name = 'João';
$userNote->age = 'idade';
$userNote->addr = 43;
class User
{
public string $name;
public int $age;
public Addr $addr;
//public parent //no caso de extends
//public self //no caso de objeto dentro da classe
}
$user = new User();
$user->name = 'João';
$user->age = 43;
$user->addr = new Addr();
$user->addr->fullAddr = ' endereço ';
varDump([$user]);
//1h01 do video
echo "<h3>Underscors Numbers Separators</h3>";
echo 1_000_000_000;
echo "<h3>ARROW FUNCTIONS</h3>";
$firstName = function (string $fullName): string {
return explode(' ', $fullName)[0];
};
echo $firstName('José Antônio da Silva'), '<hr>';
$fullName = "Karina Guimarães";
$firstName = fn () => explode(' ', $fullName)[0];
echo $firstName($fullName), '<hr>';
$firstName = fn ($fullName) => explode(' ', $fullName)[0];
echo $firstName('Sophia da Silva'), '<hr>';
echo "<h1>PHP 8</h1>";
echo 'ver: reference union and mixed types, match, new functions, array inside parts ';
//types: string|int|float|bool|null|array|object|callable|resource
echo "<h3>JUST IN TIME (JIT)</h3>";
/*
php ini
opcache.enable=1
opcache.jit_buffer_size= 100M
*/
varDump(true);
echo "<h3>CONSTRUCTOR PROMOTION PROPERTIES</h3>";
class CarA
{
public string $model;
public int $year;
function __construct(string $model, int $year)
{
$this->model = $model;
$this->year = $year;
}
}
$carA = new CarA('Fiesta', 2011);
//var_dump($carA);
varDump($carA);
class CarB
{
public function __construct(
public string $model,
public int $year
) {
}
}
$carB = new CarB('Toro', 2023);
varDump($carB);
echo "<h3>NAMED PARAMETERS</h3>";
class CarC
{
public function __construct(
public string $model,
public string $brand = 'Vw',
public ?int $year = null
) {
}
}
$carC = new CarC(model: 'Passat', year: 2023);
varDump($carC);
function myCar(
$model,
$brand = 'VW',
?int $year = null
): array {
return compact('model', 'brand', 'year');
}
varDump(myCar(model: 'Palio', brand: 'FIAT', year: 2010));
echo "<h1>PHP 8.1</h1>";
echo "<h3>ENUMS</h3>";
class OrderTest
{
private int $id;
private int|float $amount = 0.00;
private string $status;
public function createOrder(
int $id,
float|int $amount,
string $status
) {
$this->id = $id;
$this->amount = $amount;
$this->status = $status;
return $this;
}
}
$order = new OrderTest();
varDump($order->createOrder(1, 25.40, 'approved'));
enum PaymentStatus: int
{
case approved = 1;
case billet_printed = 2;
case cancelled = 3;
case chargeback = 4;
case complete = 5;
public function isPay(): bool
{
return match ($this) {
PaymentStatus::approved,
PaymentStatus::complete => true,
default => false
};
}
}
$teste = new PaymentStatus;
$desc = 'approved';
echo 'Aprovado', $teste->$desc->value(), '<hr>';
class Order
{
public function __construct(
private readonly int $id,
private int|float $amount,
private PaymentStatus $status
) {
}
public function getStatus(): PaymentStatus
{
return $this->status;
}
}
$order = new Order(2, 255.88, PaymentStatus::complete);
varDump(
$order,
$order->getStatus(),
$order->getStatus()->isPay(),
[
PaymentStatus::cases(),
PaymentStatus::from(2),
PaymentStatus::from(3)->name,
PaymentStatus::tryFrom(6),
PaymentStatus::approved->value
]
);
echo "<h1>PHP 8.2</h1>";
echo 'ver: disjoint normal form, stand-alone types, readonly class';
echo "<h3>TRAIT CONSTANT</h3>";
const DB_HOST = 'localhost';
const DB_NAME = 'fsphp';
const DB_USER = 'root';
//varDump(get_defined_constants(true))['user'];
trait dbConfig
{
protected $HOST = 'localhost';
protected $NAME = 'fsphp';
protected $USER = 'root';
protected $PASS = 'root';
}
//use dbConfig;
class dbConnect
{
public function __construct()
{
varDump(
[
self::$HOST,
self::$NANE,
self::$USER,
self::$PASS
]
);
}
}
$db = new dbConnect();
varDump($db);
echo "<h3>DEPRECATIONS</h3>";
echo "<h3>DYNAMIC PROPERTIES</h3>";
#[AllowDynamicProperties]
class House
{
public string $addr;
}
$house = new House();
$house->addrs = 'Rua x, 255';
varDump($house);
echo "<h3>UTF8 ENCODE & DECODE</h3>";
$string = 'Crase não é um acento, é um sinal que indique a fusão do artigo "a" com a preposição "a"';
varDump([
$string,
utf8_encode($string),
utf8_decode($string),
$encode = mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1'),
$decode = mb_convert_encoding($encode, 'ISO-8859-1', 'UTF-8'),
$encode, $decode
]);
echo "<div style=margin:500px></div>";
function varDump($var, ...$var2)
{
echo '<pre>', var_dump($var, ...$var2), '</pre><hr>';
}