* Removed the examples from migration and placed them in their respective sections
* Started to refactor and make the markup for the migration53 guide alot nicer and more structured
* Documented Closure assignment, and how to deal with parent scoping in closures
* Documented some heredoc related features as of 5.3


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@282526 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Kalle Sommer Nielsen 2009-06-21 19:59:34 +00:00
parent 56be1041e5
commit 6ccf275113
3 changed files with 272 additions and 267 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<appendix xml:id="migration53" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Migrating from PHP 5.2.x to PHP 5.3.x</title>
<para>
@ -19,20 +19,26 @@
versions in production environments.
</para>
<para>
If the system is being upgraded from PHP 5.1.x, the manual section titled
<link linkend="migration52">Upgrade Notes for PHP 5.2.x</link>
should also be read.
</para>
<para>
If the system is being upgraded from PHP 5.0.x, the manual section titled
<link linkend="migration51">Upgrade Notes for PHP 5.1.x</link>
should also be read.
</para>
<para>
Similarly, if the system is being upgraded from PHP 4, the manual section
titled <link linkend="migration5">Migrating from PHP 4 to PHP 5</link>
should be read as well.
If the system is being upgraded from an older version of PHP, then the
relevant documentation is available below:
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="migration52">Upgrade Notes for PHP 5.2.x</link>.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="migration51">Upgrade Notes for PHP 5.1.x</link>.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="migration5">Migrating from PHP 4 to PHP 5</link>.
</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.incompatible">
@ -112,14 +118,6 @@
</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.new-features">
<title>New features</title>
<para>
PHP 5.3.0 contains a wide set of new features, below lists
new features available to the language and syntax.
</para>
<para>
The following keywords are now reserved and may not be used
in function, class etc. names.
@ -136,211 +134,78 @@
</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.new-features">
<title>New features</title>
<para>
Support for <link linkend="language.namespaces">namespaces</link> has been
added.
PHP 5.3.0 contains a wide set of new features, below lists new features
available to the language and syntax.
</para>
<para>
Support for <link linkend="language.oop5.late-static-bindings">Late Static Bindings</link>
has been added.
</para>
<para>
Support for a jump label (limited goto) has been added. Its only possible to jump
in the same context and out of loops, but not into loops, or other scopes/contexts.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Basic usage
goto end;
echo 'This will never be printed!';
end:
echo 'This is the end';
// This wont work, as we cannot jump into another scope
goto baz;
function foobar()
{
baz:
{
// ...
}
}
// Loops
foreach(range('a', 'c') as $character)
{
if($character == 'b')
{
goto done;
}
}
done:
echo 'Character "b" reached, stopping';
// This wont work, as we cannot jump into loops
goto impossible;
foreach(range('a', 'c') as $character)
{
impossible:
{
// ...
}
}
?>
]]>
</programlisting>
</informalexample>
<para>
PHP now contains support for native <link linkend="functions.anonymous">closures</link>
(Lambda, Anonymous functions). These are implemented using a new reserved class,
<classname>Closure</classname>. Closures can be used in any function that takes a
<link linkend="language.types.callback">callback</link> as parameter.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Simple closure
$greet = function($who)
{
echo 'Hello ' . $who . PHP_EOL;
}
$greet('Kalle');
$greet('Rasmus');
// Using as a callback
$names = Array('KALLE', 'RASMUS', 'PIERRE');
$callback = function($element)
{
return lcfirst($element);
}
// Prints:
// kALLE
// rASMUS
// pIERRE
var_dump(array_map($names, $callback));
// The above could also have been done by declaring the
// closure directly in the parameter
var_dump(array_map($names, function($element)
{
return lcfirst($element);
}));
?>
]]>
</programlisting>
</informalexample>
<para>
Theres two new magic methods,
<link linkend="language.oop5.overloading.methods">__callStatic</link> and
<link linkend="language.oop5.magic.invoke">__invoke</link>. The magic
<literal>__invoke</literal> method makes it possible for a class instance
to be callable, thrus making it possible to pass it to a parameter of the
<link linkend="language.types.callback">callback</link> type.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class Callable
{
function __invoke()
{
echo 'Invoked!' . PHP_EOL;
}
}
$callable = new Callable;
// Works like a closure
$callable();
// And as callbacks
call_user_func($callable);
?>
]]>
</programlisting>
</informalexample>
<para>
<link linkend="language.types.string.syntax.nowdoc">NOWDOC</link> syntax is now
supported, and works like HEREDOC but with single quotes:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo <<<'GREET'
Hello World
GREET;
?>
]]>
</programlisting>
</informalexample>
<para>
Static HEREDOCs can be used to initialize static variables, class members
or class constants:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function foo()
{
static $bar = <<<LABEL
No variables here...
LABEL;
}
class bar
{
const $bar = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}
?>
]]>
</programlisting>
</informalexample>
<para>
Its now possible to declare constants using the <literal>const</literal>
keyword:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
const MY_CONSTANT = 'Hello';
define('MY_OTHER_CONSTANT', 'World');
echo MY_CONSTANT . ' ' . MY_OTHER_CONSTANT;
?>
]]>
</programlisting>
</informalexample>
<para>
The <link linkend="language.operators.comparison.ternary">ternary</link> operator
now have a shorthand syntax:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
var_dump(0 ?: 'Hello!');
]]>
</programlisting>
</informalexample>
<itemizedlist>
<listitem>
<para>
Support for <link linkend="language.namespaces">namespaces</link> has been
added.
</para>
</listitem>
<listitem>
<para>
Support for <link linkend="language.oop5.late-static-bindings">Late Static Bindings</link>
has been added.
</para>
</listitem>
<listitem>
<para>
Support for a <link linkend="control-structures.goto">jump label</link> (limited goto)
has been added.
</para>
</listitem>
<listitem>
<para>
Support for native <link linkend="functions.anonymous">Closures</link>
(Lambda/Anonymous functions) has been added.
</para>
</listitem>
<listitem>
<para>
Theres two new magic methods,
<link linkend="language.oop5.overloading.methods">__callStatic</link> and
<link linkend="language.oop5.magic.invoke">__invoke</link> has been added.
</para>
</listitem>
<listitem>
<para>
<link linkend="language.types.string.syntax.nowdoc">Nowdoc</link> syntax is now
supported, and works like <link linkend="language.types.string.syntax.heredoc">Heredoc</link>
but with single quotes.
</para>
</listitem>
<listitem>
<para>
Its not possible to use <link linkend="language.types.string.syntax.heredoc">Heredoc</link>
to initialize static variables and class members/constants.
</para>
</listitem>
<listitem>
<para>
<link linkend="language.types.string.syntax.heredoc">Heredoc</link> syntax may now be declared
using double quotes.
</para>
</listitem>
<listitem>
<para>
<link linkend="language.constants">Constants</link> can now be declared outside a class
declaring using the <literal>const</literal> keyword.
</para>
</listitem>
<listitem>
<para>
The <link linkend="language.operators.comparison.ternary">ternary</link> operator
now have a shorthand operator <literal>?:</literal>.
</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.windows">
@ -1094,7 +959,7 @@ var_dump(0 ?: 'Hello!');
<listitem>
<simpara>
<function>PDO::setAttribute</function>
- Set an attribute.
- Sets an attribute.
</simpara>
</listitem>
</itemizedlist>
@ -1156,6 +1021,48 @@ var_dump(0 ?: 'Hello!');
</itemizedlist>
</section>
<section xml:id="migration53.new-extensions">
<title>New Extensions</title>
<para>
The following are new extensions added (by default) as of PHP 5.3.0:
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="book.fileinfo">Fileinfo</link>
- Improved and more solid replacement for the
<link linkend="book.mime-magic">Mimetype</link> extension.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.intl">INTL</link>
- Internationalization extension. INTL is a wrapper around the
<link xlink:href="&url.icu.home;">ICU</link> library.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.phar">Phar</link>
- Implementation of PHP-Archive files.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.sqlite3">SQLite3</link>
- Support for SQLite version 3 databases.
</simpara>
</listitem>
</itemizedlist>
<para>
mysqlnd is a new core library shipped with PHP. It is a PHP-specific
replacement for libmysql. mysqlnd will be used to build the
<link linkend="book.mysql">mysql</link>, <link linkend="book.mysqli">mysqli</link>
and <link linkend="ref.pdo-mysql">PDO_MYSQL</link> if libmysql isnt found
on the system, but it may also be used instead of libmysql.
</para>
</section>
<section xml:id="migration53.removed-extensions">
<title>Removed Extensions</title>
<para>
@ -1210,40 +1117,6 @@ var_dump(0 ?: 'Hello!');
</itemizedlist>
</section>
<section xml:id="migration53.new-extensions">
<title>New Extensions</title>
<para>
The following are new extensions added (by default) as of PHP 5.3.0:
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="book.fileinfo">Fileinfo</link>
- Improved and more solid replacement for the
<link linkend="book.mime-magic">Mimetype</link> extension.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.intl">INTL</link>
- Internationalization extension. INTL is a wrapper around the
<link xlink:href="&url.icu.home;">ICU</link> library.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.phar">Phar</link>
- Implementation of PHP-Archive files.
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="book.sqlite3">SQLite3</link>
- Support for SQLite version 3 databases.
</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.extensions-other">
<title>Other changes to extensions</title>
@ -1305,10 +1178,6 @@ var_dump(0 ?: 'Hello!');
</simpara>
</listitem>
</itemizedlist>
<para>
mysqlnd is a new core library shipped with PHP. It is a PHP-specific
replacement for libmysql.
</para>
</section>
<section xml:id="migration53.classes">
@ -1903,7 +1772,6 @@ var_dump(0 ?: 'Hello!');
</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="migration53.other">

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.70 $ -->
<!-- $Revision: 1.71 $ -->
<chapter xml:id="language.functions" xmlns="http://docbook.org/ns/docbook">
<title>Functions</title>
@ -623,6 +623,85 @@ echo preg_replace_callback('~-([a-z])~', function ($match) {
}, 'hello-world');
// outputs helloWorld
?>
]]>
</programlisting>
</example>
</para>
<para>
Closures can also be declared to values of variables, which PHP magiclly
turns into an object instance of the <classname>Closure</classname>
class. When assigning a closure to a variable, its done by a basic
assignment to a function declaring with an ending semicolon like any
other assignments
</para>
<para>
<example>
<title>Anonymous function variable assignment example</title>
<programlisting role="php">
<![CDATA[
<?php
$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World');
$greet('PHP');
?>
]]>
</programlisting>
</example>
</para>
<para>
Closures may also inherit variables from the parent scope, to do so it
must be declared in the function header, unlike regular functions that
uses the <literal>global</literal> keyword.
</para>
<para>
<example>
<title>Closures and scoping</title>
<programlisting role="php">
<![CDATA[
<?php
// Declare a basic shopping card that allows us to
// add products to it, with a member that allows us
// to get the quantity of a product we have added using
// a callback function
class Card
{
public static $products = Array();
public static function add($product, $quantity)
{
self::$products[$product] = $quantity;
}
public static function getQuantity($product)
{
$quantity = false;
array_map(function($value, $key) use($product, &$quantity)
{
if($key == $product)
{
$quantity = $value;
}
}, array_values(self::$products), array_keys(self::$products));
return($quantity);
}
}
// Add some items to our card
Card::add('Butter', 1);
Card::add('Milk', 3);
Card::add('Eggs', 6);
// Now get the total 'Eggs' we added
printf('The card contains %d eggs', Card::getQuantity('Eggs'));
?>
]]>
</programlisting>
</example>
@ -637,6 +716,12 @@ echo preg_replace_callback('~-([a-z])~', function ($match) {
Anonymous functions are available since PHP 5.3.0.
</para>
</note>
<note>
<para>
Its possible to use the <function>func_get_args</function> and
family functions within a closure.
</para>
</note>
</sect1>
</chapter>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.21 $ -->
<!-- $Revision: 1.22 $ -->
<sect1 xml:id="language.types.string">
<title>Strings</title>
@ -319,6 +319,58 @@ EOD
</programlisting>
</example>
<para>
As of PHP 5.3.0, its possible to initialize static variables and class
members/constants using the Heredoc syntax:
</para>
<example>
<title>Using Heredoc to initialize static values</title>
<programlisting role="php">
<![CDATA[
<?php
// Static variables
function foo()
{
static $bar = <<<LABEL
Nothing in here...
LABEL;
}
// Class members/constants
class foo
{
const BAR = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}
?>
]]>
</programlisting>
</example>
<para>
PHP 5.3.0 also introduces the possibility for Heredoc's to
use double quotes in declarings:
</para>
<example>
<title>Using double quotes in Heredoc</title>
<programlisting role="php">
<![CDATA[
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
]]>
</programlisting>
</example>
<note>
<para>
Heredoc support was added in PHP 4.