Modern PHP: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
No edit summary
Line 11: Line 11:


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).
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 <code>__autoload()</code>. This is no longer recommended and is deprecated in PHP 7.x.
The successor to <code>__autoload()</code> is <code>spl_autoload_register()</code>. 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 ==
== Built-in web server ==

Revision as of 13:12, 18 April 2018

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

Links