Modern PHP: Difference between revisions
No edit summary |
|||
Line 4: | Line 4: | ||
<code>composer install</code> will install the exact versions specified in <code>composer.lock</code>. This ensures that all developers working on the same project are using the same versions. <code>composer update</code> will update all installed packages to their latest versions and update the <code>composer.lock</code> file. | <code>composer install</code> will install the exact versions specified in <code>composer.lock</code>. This ensures that all developers working on the same project are using the same versions. <code>composer update</code> will update all installed packages to their latest versions and update the <code>composer.lock</code> file. | ||
== Project directory structure == | |||
In the root directory: | |||
* <code>src</code> - PHP source code (classes). | |||
* <code>tests</code> - PHPUnit tests. | |||
* <code>composer.json</code> - Composer dependencies and configuration. | |||
* <code>phpunit.xml</code> - PHPUnit configuration. | |||
* <code>.travis.yml</code> - Continuous integration configuration. | |||
== Generators == | == Generators == |
Revision as of 08:43, 29 May 2018
Composer
You should commit composer.json
and composer.lock
into version control.
composer install
will install the exact versions specified in composer.lock
. This ensures that all developers working on the same project are using the same versions. composer update
will update all installed packages to their latest versions and update the composer.lock
file.
Project directory structure
In the root directory:
src
- PHP source code (classes).tests
- PHPUnit tests.composer.json
- Composer dependencies and configuration.phpunit.xml
- PHPUnit configuration..travis.yml
- Continuous integration configuration.
Generators
Generators are similar to iterators that calculate their values on demand, thus potentially using fewer resources. They can be created using the yield
keyword, e.g.:
function my_generator() { yield 1; yield 2; yield 3; }
Generators are forward-only iterators, i.e. you can only request the next value, not the previous, next but one etc. They are useful for iterating sequential data sets such as reading a file line by line (in order).
Autoloading
The original mechanism for autoloading was the magic function __autoload()
. This is no longer recommended and is deprecated in PHP 7.x.
The successor to __autoload()
is spl_autoload_register()
. This in turn has been replaced by PSR-0, then PSR-4. Composer will generate a PSR-4 autoloader for you.
Built-in web server
PHP comes with a built-in web server which is useful for development purposes, although it should never be used in production.
- Configuration directives:
-c config/php.ini
Useful dependencies
- FastRoute
aura/router
orleague/route
aura/filter
,respect/validation
,symfony/validator
mbstring
extension, providesmb_
functions such asmb_strlen
filp/whoops
- pretty printing of exceptions and errorsmonolog/monolog