Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion language/control-structures/finally.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 6af4cd5be387fcb28cabfca49f0d9109ebfc660f Maintainer: lacatoire Status: ready -->
<!-- EN-Revision: 43127142d0d96b6a49d4bdcc6ca1a72d0e55b7a0 Maintainer: lacatoire Status: ready -->
<!-- Reviewed: no -->
<sect1 xml:id="control-structures.finally" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>finally</title>
Expand Down Expand Up @@ -182,6 +182,56 @@ finally catch
]]>
</screen>
</example>
<simpara>
Una sentencia &return; en un bloque &finally; también descarta cualquier
excepción que se esté propagando cuando se ejecuta el bloque &finally;:
una excepción lanzada en el bloque &try; que no es capturada,
o una excepción lanzada o relanzada en un bloque &catch;.
No se informa de ningún error, y la función devuelve el valor del bloque
&finally; como si no se hubiera lanzado ninguna excepción.
</simpara>
<example>
<title>Un &return; en un bloque &finally; descarta una excepción relanzada</title>
<programlisting role="php">
<![CDATA[
<?php
function process() {
try {
throw new Exception('Excepción original');
} catch (Exception $e) {
echo 'Capturada: ', $e->getMessage(), "\n";
throw $e;
} finally {
echo 'Dentro de finally', "\n";
return 'finally';
}
}

try {
echo process(), "\n";
} catch (Exception $e) {
echo 'Esto nunca se alcanza', "\n";
}
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Capturada: Excepción original
Dentro de finally
finally
]]>
</screen>
</example>
<simpara>
A diferencia de &return;, salir de un bloque &finally; con una sentencia
<link linkend="control-structures.break"><literal>break</literal></link>,
<link linkend="control-structures.continue"><literal>continue</literal></link>
o <link linkend="control-structures.goto"><literal>goto</literal></link>
no está permitido, y produce un error fatal en tiempo de compilación.
Los bucles contenidos por completo dentro del bloque &finally; pueden seguir
utilizando <literal>break</literal> y <literal>continue</literal>.
</simpara>
</sect2>

<sect2>
Expand Down
149 changes: 148 additions & 1 deletion language/oop5/property-hooks.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: dff848b17dafaabfb7662ffb5fecfcb04e3b771d Maintainer: PhilDaiguille Status: ready -->
<!-- EN-Revision: 3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59 Maintainer: PhilDaiguille Status: ready -->
<sect1 xml:id="language.oop5.property-hooks" xmlns="http://docbook.org/ns/docbook">
<title>Hooks de propiedad</title>

Expand Down Expand Up @@ -410,6 +410,153 @@ class Person {
Trabajar solo con elementos del array requiere atención especial.
</simpara>
</sect2>
<sect2 xml:id="language.oop5.property-hooks.isset-unset">
<title><function>isset</function>, <function>empty</function> y <function>unset</function></title>
<simpara>
Cuando se utiliza <function>isset</function> o <function>empty</function> sobre una
propiedad que tiene un hook <literal>get</literal>, se llama al hook y
se comprueba el valor que devuelve en lugar del valor de respaldo de la propiedad.
<function>isset</function> devuelve &true; si ese valor no es &null;,
y <function>empty</function> devuelve &true; si ese valor es igual a &false;.
Esto es intencionado: un hook <literal>get</literal> puede calcular o inicializar
el valor cuando se lee, de modo que el código externo a los hooks de la propiedad
no puede observar directamente el valor de respaldo.
</simpara>
<simpara>
Como consecuencia, si el hook <literal>get</literal> lee un valor de respaldo
que no ha sido inicializado, utilizar <function>isset</function> o
<function>empty</function> sobre la propiedad lanza un <exceptionname>Error</exceptionname>,
igual que lo hace la lectura de la propiedad.
Lo mismo se aplica al
<link linkend="language.operators.comparison.coalesce">operador de fusión de null</link>.
</simpara>
<example>
<title><function>isset</function> sobre una propiedad con un hook <literal>get</literal></title>
<programlisting role="php">
<![CDATA[
<?php
class Article
{
public ?string $title = null {
get => $this->title ?? 'Untitled';
}

public string $author {
get => ucfirst($this->author);
set => $this->author = $value;
}
}

$article = new Article();

// El valor de respaldo es null, pero el hook get devuelve 'Untitled'.
var_dump(isset($article->title));

// El hook get lee un valor de respaldo no inicializado.
try {
var_dump(isset($article->author));
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
Typed property Article::$author must not be accessed before initialization
]]>
</screen>
</example>
<simpara>
Si una propiedad "backed" no tiene un hook <literal>get</literal>,
<function>isset</function> y <function>empty</function> comprueban directamente
el valor de respaldo, como hacen con una propiedad sin hooks.
En una propiedad virtual que no tiene un hook <literal>get</literal>,
lanzan un <exceptionname>Error</exceptionname>, porque la propiedad no se puede leer.
</simpara>
<simpara>
Dentro de los hooks de una propiedad, <code>$this->propName</code> hace referencia al
valor de respaldo de esa propiedad, por lo que <code>isset($this->propName)</code> comprueba
directamente el valor de respaldo.
Un hook <literal>get</literal> puede utilizarlo para gestionar un valor de respaldo no inicializado.
A menudo es más sencillo dar a la propiedad un valor por omisión, como &null;.
</simpara>
<example>
<title>Gestión de un valor de respaldo no inicializado</title>
<programlisting role="php">
<![CDATA[
<?php
class Article
{
// Inicializar el valor de respaldo evita el error.
public ?string $subtitle = null {
get => $this->subtitle !== null ? ucfirst($this->subtitle) : null;
}

// El hook get comprueba su propio valor de respaldo.
public string $author {
get => isset($this->author) ? ucfirst($this->author) : 'Anonymous';
set => $this->author = $value;
}
}

$article = new Article();

var_dump(isset($article->subtitle));
var_dump(isset($article->author));
var_dump($article->author);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(false)
bool(true)
string(9) "Anonymous"
]]>
</screen>
</example>
<simpara>
Una propiedad que tiene hooks no se puede destruir.
Utilizar <function>unset</function> sobre ella lanza un <exceptionname>Error</exceptionname>,
tanto si la propiedad es "backed" como virtual, e incluso cuando
<function>unset</function> se llama desde dentro de la clase o de uno de sus hooks.
</simpara>
<example>
<title><function>unset</function> sobre una propiedad con hooks</title>
<programlisting role="php">
<![CDATA[
<?php
class Rectangle
{
public int $area {
get => $this->h * $this->w;
}

public function __construct(public int $h, public int $w) {}
}

$r = new Rectangle(4, 5);

try {
unset($r->area);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Cannot unset hooked property Rectangle::$area
]]>
</screen>
</example>
</sect2>
<sect2>
<title>Herencia</title>
<sect3>
Expand Down
11 changes: 10 additions & 1 deletion reference/var/functions/empty.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: e6e9c116083c3d1aeb3d1fc2e45f51fbcb0d5182 Maintainer: PhilDaiguille Status: ready -->
<!-- EN-Revision: 3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59 Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: yes -->
<refentry xml:id="function.empty" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
Expand Down Expand Up @@ -150,6 +150,15 @@ bool(false)
será llamado, si existe.
</para>
</note>
<note>
<simpara>
Al utilizar <function>empty</function> sobre una propiedad que tiene un hook
<literal>get</literal>, se llama al hook y se comprueba el valor que devuelve.
Consulte los
<link linkend="language.oop5.property-hooks.isset-unset">hooks de propiedad</link>
para más detalles.
</simpara>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
Expand Down
11 changes: 10 additions & 1 deletion reference/var/functions/isset.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: dd100db2cb8b7d7384e5daf3a1818ed090b1ad5a Maintainer: PhilDaiguille Status: ready -->
<!-- EN-Revision: 3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59 Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: yes -->

<refentry xml:id="function.isset" xmlns="http://docbook.org/ns/docbook">
Expand Down Expand Up @@ -182,6 +182,15 @@ bool(false)
si existe.
</para>
</note>
<note>
<simpara>
Al utilizar <function>isset</function> sobre una propiedad que tiene un hook
<literal>get</literal>, se llama al hook y se comprueba el valor que devuelve.
Consulte los
<link linkend="language.oop5.property-hooks.isset-unset">hooks de propiedad</link>
para más detalles.
</simpara>
</note>
</refsect1>

<refsect1 role="seealso">
Expand Down
10 changes: 9 additions & 1 deletion reference/var/functions/unset.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: d816a0fad6c458d9515f697cc89e26ca9d8069f5 Maintainer: PhilDaiguille Status: ready -->
<!-- EN-Revision: 3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59 Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: yes -->

<refentry xml:id="function.unset" xmlns="http://docbook.org/ns/docbook">
Expand Down Expand Up @@ -210,6 +210,14 @@ unset($foo1, $foo2, $foo3);
será llamado, si existe.
</para>
</note>
<note>
<simpara>
Utilizar <function>unset</function> sobre una propiedad que tiene hooks
lanza un <exceptionname>Error</exceptionname>. Consulte los
<link linkend="language.oop5.property-hooks.isset-unset">hooks de propiedad</link>
para más detalles.
</simpara>
</note>
</refsect1>

<refsect1 role="seealso">
Expand Down
Loading