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
130 changes: 130 additions & 0 deletions language/oop5/final.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 907f8ab64150c8503cb9f33d274ab6a7211b86a3 Maintainer: lacatoire Status: ready -->
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
<title>La parola chiave final</title>
<para>
La parola chiave final impedisce alle classi figlie di sovrascrivere un metodo, una proprietà o una costante
anteponendo alla definizione il prefisso <literal>final</literal>. Se la classe
stessa viene definita come final, non potrà essere estesa.
</para>
<para>
<example>
<title>Esempio di metodi final</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}

final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}

class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// Genera un errore fatale: Cannot override final method BaseClass::moreTesting()
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Esempio di classe final</title>
<programlisting role="php">
<![CDATA[
<?php
final class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}

// Poiché la classe è già final, la parola chiave final è ridondante
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}

class ChildClass extends BaseClass {
}
// Genera un errore fatale: Class ChildClass may not inherit from final class (BaseClass)
?>
]]>
</programlisting>
</example>
</para>
<example>
<title>Esempio di proprietà final a partire da PHP 8.4.0</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
final protected string $test;
}

class ChildClass extends BaseClass {
public string $test;
}
// Genera un errore fatale: Cannot override final property BaseClass::$test
?>
]]>
</programlisting>
</example>
<example xml:id="language.oop5.final.example.php81">
<title>Esempio di costanti final a partire da PHP 8.1.0</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo
{
final public const X = "foo";
}

class Bar extends Foo
{
public const X = "bar";
}

// Fatal error: Bar::X cannot override final constant Foo::X
?>
]]>
</programlisting>
</example>

<note>
<simpara>
A partire da PHP 8.0.0, i metodi privati non possono essere dichiarati final, a eccezione del <link linkend="language.oop5.decon.constructor">costruttore</link>.
</simpara>
</note>
<note>
<simpara>
Una proprietà dichiarata <link linkend="language.oop5.visibility-members-aviz"><literal>private(set)</literal></link> è implicitamente <literal>final</literal>.
</simpara>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
105 changes: 105 additions & 0 deletions language/oop5/iterations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 1fb0ef23d7be0d8ecd9604fce16ee1e0842c6ef6 Maintainer: lacatoire Status: ready -->
<sect1 xml:id="language.oop5.iterations" xmlns="http://docbook.org/ns/docbook">
<title>Iterazione degli oggetti</title>
<para>

PHP fornisce un modo per definire gli oggetti in modo tale che sia possibile iterare
attraverso un elenco di elementi, ad esempio con un'istruzione &foreach;. Per impostazione predefinita,
tutte le proprietà <link linkend="language.oop5.visibility">visibili</link> verranno utilizzate
per l'iterazione.

</para>

<example>
<title>Iterazione semplice degli oggetti</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass
{
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';

protected $protected = 'protected var';
private $private = 'private var';

function iterateVisible() {
echo "MyClass::iterateVisible:\n";
foreach ($this as $key => $value) {
print "$key => $value\n";
}
}
}

$class = new MyClass();

foreach($class as $key => $value) {
print "$key => $value\n";
}
echo "\n";


$class->iterateVisible();

?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
var1 => value 1
var2 => value 2
var3 => value 3

MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var
]]>
</screen>

</example>

<para>
Come mostra l'output, il &foreach; ha iterato attraverso tutte le
proprietà <link linkend="language.oop5.visibility">visibili</link> a cui era possibile
accedere.
</para>

<simplesect role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.generators">Generatori</link></member>
<member><interfacename>Iterator</interfacename></member>
<member><interfacename>IteratorAggregate</interfacename> </member>
<member><link linkend="spl.iterators">Iteratori SPL</link></member>
</simplelist>
</para>
</simplesect>

</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
124 changes: 124 additions & 0 deletions language/oop5/object-comparison.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: a9edd62d087ab1eb6292c795b7256e14ff9f1234 Maintainer: lacatoire Status: ready -->
<sect1 xml:id="language.oop5.object-comparison" xmlns="http://docbook.org/ns/docbook">
<title>Confronto tra oggetti</title>
<para>
Quando si utilizza l'operatore di confronto (<literal>==</literal>),
le variabili oggetto vengono confrontate in modo semplice, ovvero: due istanze
di oggetto sono uguali se hanno gli stessi attributi e valori (i valori vengono confrontati con <literal>==</literal>) e sono
istanze della stessa classe.
</para>
<para>
Quando si utilizza l'operatore di identità (<literal>===</literal>),
le variabili oggetto sono identiche se e solo se si riferiscono alla stessa
istanza della stessa classe.
</para>
<para>
Un esempio chiarirà queste regole.
<example>
<title>Esempio di confronto tra oggetti</title>
<programlisting role="php">
<![CDATA[
<?php
function bool2str($bool)
{
if ($bool === false) {
return 'FALSE';
} else {
return 'TRUE';
}
}

function compareObjects(&$o1, &$o2)
{
echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n";
echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n";
echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n";
}

class Flag
{
public $flag;

function __construct($flag = true) {
$this->flag = $flag;
}
}

class OtherFlag
{
public $flag;

function __construct($flag = true) {
$this->flag = $flag;
}
}

$o = new Flag();
$p = new Flag();
$q = $o;
$r = new OtherFlag();

echo "Two instances of the same class\n";
compareObjects($o, $p);

echo "\nTwo references to the same instance\n";
compareObjects($o, $q);

echo "\nInstances of two different classes\n";
compareObjects($o, $r);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Two instances of the same class
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Two references to the same instance
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Instances of two different classes
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE
]]>
</screen>
</example>
</para>
<note>
<para>
Le estensioni possono definire le proprie regole per il confronto dei loro oggetti
(<literal>==</literal>).
</para>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
Loading