PHP Notes

You can find useful tutorials online for Laravel in the Laravel documentation, Laracasts discussion forum, Treehouse, Sitepoint, and Codebright.

The Laravel Github docs are also extensive and provide excellent information that Symfony doesn’t provide.

Книга Symfony:

Symfony profiling:

https://symfony.com/doc/current/profiler.html?ref=hackernoon.com

Как использовать yield?:

yield + return

https://medium.com/just-tech/working-with-php-generators-57586e61f19a

$gen = (function() {
    yield 1;
    yield 2;
    // 3 is only returned after the generator has finished
    return 3;

})();

foreach ($gen as $val) {
    echo $val, PHP_EOL;
}

echo $gen->getReturn(), PHP_EOL;

// Outputs 1 2 3

Как использовать Generator?:

https://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html

https://medium.com/just-tech/working-with-php-generators-57586e61f19a

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.

<?php
function xrange($start, $limit, $step = 1) {
    if ($start <= $limit) {
        if ($step <= 0) {
            throw new LogicException('Step must be positive');
        }

        for ($i = $start; $i <= $limit; $i += $step) {
            yield $i;
        }
    } else {
        if ($step >= 0) {
            throw new LogicException('Step must be negative');
        }

        for ($i = $start; $i >= $limit; $i += $step) {
            yield $i;
        }
    }
}

/*
 * Note that both range() and xrange() result in the same
 * output below.
 */

echo 'Single digit odd numbers from range():  ';
foreach (range(1, 9, 2) as $number) {
    echo "$number ";
}
echo "\n";

echo 'Single digit odd numbers from xrange(): ';
foreach (xrange(1, 9, 2) as $number) {
    echo "$number ";
}
?>

Что за конструкция static function (как аргумент)?

class PostsCollectionFilter
{
    public function unpublished($posts)
    {
        return $posts->filter(static function ($post) {
            return ! $post->published;
        });
    }
}

В общем статическая анонимная функция прибавляет немного производительности в отличие от обычной анонимной функции. Если нам не нужен $this в этой фнкции, то мы можем указать static, но это некритично.

It is worth noting that in context of a class, static anonymous functions yield micro performance improvements and may be suggested in any instances which you do not need the containing class to be bound to the anonymous function.

Как используются замыкания в PHP?

https://wiki.php.net/rfc/closures

Для чего нужно Late Static Bindings?

Чтобы вызвать метод в контексте не где он объявлен, а где вызван:

https://www.youtube.com/watch?v=MxRrCV_3VSQ

class Book{

    public static $name = 'Kirill';
    
    public static function author()
    {
        return "the author name is ". self::$name;
    
    }
    
    public static function getAuthor(){
        echo static::author();
    }
}

class NewBook{

    
    public static function author()
    {
        return "the author name is ". self::$name . " and K";
    
    }
    
   
}

Book::getAuthor(); //he author name is Kirill
NewBook::getAuthor(); //he author name is Kirill and K

Когда использовать immutable objects?

Посмотреть инструмент PHP metrics.

https://www.phpmetrics.org/

Тестирование:

Посмотреть, как они делаются здесь.

https://github.com/yiisoft/validator

Сайт, на котором собран 61 PHP-инструмент для анализа кода, управления зависимостями, тестов: https://phpqa.io/ А в этом репозитории ещё свыше ста линтеров, форматтеров и статических анализаторов: https://tprg.ru/lptv #php #инструментыt.me/tproger_web/1004

PHPStorm auto indent

https://plugins.jetbrains.com/plugin/7642-save-actions

Last updated