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 | |
/** | |
* Class MySingleton. | |
*/ | |
class MySingleton | |
{ | |
private static $instance; | |
private function __construct() {} |
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
function flatten($items) | |
{ | |
$result = []; | |
foreach ($items as $item) { | |
if (is_array($item)) { | |
$result = array_merge($result, array_values($item)); | |
continue; | |
} | |
$result[] = $item; |
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 isPrime(int $number): bool | |
{ | |
$boundary = floor(sqrt($number)); | |
$i = 2; | |
do { | |
if (($number % $i) === 0) { | |
return false; | |
} |
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 | |
$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'); |
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 writeln($line_in) { | |
echo $line_in." \n"; | |
} | |
class Test { | |
public $number; | |
function __construct(int $number) | |
{ |
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 | |
# 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); |
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 | |
# 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); |
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 | |
# 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...'; |
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 | |
# 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; |
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 | |
namespace App\Repository; | |
use App\Entity\YourEntity; | |
use Doctrine\{Common\Collections\Criteria, ORM\QueryBuilder}; | |
use Knp\Component\Pager\Pagination\PaginationInterface; | |
/** | |
* class BaseYourEntityRepositoryDecorator. |
OlderNewer