Skip to content

Instantly share code, notes, and snippets.

View woprrr's full-sized avatar

Alexandre Mallet woprrr

View GitHub Profile
@woprrr
woprrr / BaseYourEntityRepositoryDecorator.php
Created January 22, 2021 11:58
Fast example of Symfony 4.* + Repository Service Decorator
<?php
namespace App\Repository;
use App\Entity\YourEntity;
use Doctrine\{Common\Collections\Criteria, ORM\QueryBuilder};
use Knp\Component\Pager\Pagination\PaginationInterface;
/**
* class BaseYourEntityRepositoryDecorator.
@woprrr
woprrr / gist.php
Created March 5, 2020 10:02
PHP 7.4 Typed Properties examples. Try it now : https://3v4l.org/iFsCM
<?php
# Typed Properties 2.0
# OLD WAY (PHP < 7.4 )
class User {
public $id;
public $name;
public function __construct(int $id, string $name) {
$this->id = $id;
@woprrr
woprrr / gist.php
Created March 5, 2020 09:52
Null Coalescing Assignment Operator examples. Try it now : https://3v4l.org/pV6PF
<?php
# Null Coalescing Assignment Operator
# OLD WAY (PHP < 7.4 )
$data['comments']['user_id'] = $data['comments']['user_id'] ?? 'PlaceHoldered value...';
var_dump($data['comments']['user_id']);
$data = null;
# PHP 7.4 + syntax to use new assigment operator without unecessary tests.
$data['comments']['user_id'] ??= 'PlaceHoldered value...';
@woprrr
woprrr / gist.php
Created March 5, 2020 09:46
PHP 7.4 Arrow Functions 2.0 `Short Closures` examples. Try it now : https://3v4l.org/C3UfN
<?php
# Arrow Functions 2.0 `Short Closures`
# OLD WAY (PHP < 7.4 )
function cube($arg){
return ($arg * $arg * $arg);
}
$b = array_map('cube', range(1, 5));
var_dump('Short closure old syntax', $b);
@woprrr
woprrr / gist:76c05964b0f7daf60f515857a74e83d1
Last active March 4, 2020 18:10
PHP 7.4 Spread Operator in Array Expression examples. Try it now : https://3v4l.org/hfFZm
<?php
# Forget array_merge PHP 7.4 Spread Operator in Array Expression.
# ONLY FOR PHP 7.4 +
#/!\ PERFORMANCE BEST PRACTIVE /!\
$args = ['foo', 'bar', 'baz'];
$arr = [...$args, 'buz'];
var_dump($arr);
@woprrr
woprrr / gist:f8b975a2406c11ed4b5b62e5a47e9895
Created September 19, 2019 09:48
Closure in PHP 7.1 examples
<?php
function writeln($line_in) {
echo $line_in." \n";
}
class Test {
public $number;
function __construct(int $number)
{
@woprrr
woprrr / benchmark.php
Created September 19, 2019 08:41
Benchmark ArrayWalk VS FOREACH
<?php
$elements = [];
for($i = 0; $i < 100000; $i++) {
$elements[] = (string)rand(10000000, PHP_INT_MAX);
}
function testArrayWalk($array) {
$time_start = microtime(true);
$element = array_walk($array, 'test_walking');
@woprrr
woprrr / gist:8fb60a80d0b0583eec68a99829937ed3
Created August 20, 2019 08:50
Evaluate if number is prime.
<?php
function isPrime(int $number): bool
{
$boundary = floor(sqrt($number));
$i = 2;
do {
if (($number % $i) === 0) {
return false;
}
function flatten($items)
{
$result = [];
foreach ($items as $item) {
if (is_array($item)) {
$result = array_merge($result, array_values($item));
continue;
}
$result[] = $item;
@woprrr
woprrr / MySingleton.php
Created March 26, 2019 10:37
A dummy/simple singleton to test it.
<?php
/**
* Class MySingleton.
*/
class MySingleton
{
private static $instance;
private function __construct() {}