Archive for the ‘php’ Category

Re-indent with PhpStorm

PhpStorm->Code->Reformat Code or Ctrl+Shift+Alt+L and click Reformat file

Double ??

Before PHP 7: if (isset($inputData)) { $param = $inputData; } else { $param = ‘default’; } In PHP 7: $param = $inputData ?? ‘default’; or $param = $inputData ?? $someData ?? ‘default’;

Method chaining to split logic into a chain-look to make more understandable pieces of code

class MyCar { function setPedals() { // code logic return this; }   function setWheels() { // code logic return this; }   function setEngine() { // code logic } }   $myCar = new MyCar(); $myCar.setPedals().setWheels().setEngine(); This pattern also called Fluent Interface. More information can be found here: Method chaining Fluent interface

Phpunit Mock vs Stub

Моcк — имитация объекта Stub — заглушка для метода

Passport validation :: check digit example

  <?php   function calcCheckDigit($inputCode) { $btArray = str_split($inputCode); $total = 0;   for ($index = 0; $index < sizeof($btArray); $index++) { $btChr = ord($btArray[$index]);   if ($btChr == 60) { //convert spacer char < to 0 $btArray[$index] = 0; } else if ($btChr >= 65) { //convert letters A-Z to 10-35 $btArray[$index] = $btChr […]

PHP The static Keyword for caching and speeding up

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this use the static keyword.

PHP Recursive function

  <?php   function recursive(&$myarray, $dir, $recursiveDir = null) {   $items = scandir($dir);   foreach ($items as $item) {   if ($item != "." && $item != "..") {   $file = $dir. DIRECTORY_SEPARATOR. $item;   if (is_dir($file)) { definitions($routes, $file, $file); } else { // get name of api $expl = explode(".", $item); […]

PHP Functional Programming, helper function, annonymous function, closure

  <?php $url = function($folder) use ($hostname) { return $hostname . DIRECTORY_SEPARATOR . $folder; };   $hostname = "https://" . $_SERVER[’SERVER_NAME’];   $myurl = $url(’pictures’); echo $myurl;   // output // https://www.aghayev.com/pictures

Stateless – when the application doesn’t save anything to the disk or to the RAM that is required to process future workloads. The benefit of stateless application is that it is possible to run multiple instances of an application in order to distribute workload and increase availability.

PHP PSR-4 and Namespaces

How-to add namespaces to old php project https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md Simple project with namespaces https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md