|
| 1 | +<?xml version="1.0" encoding="utf-8"?> |
| 2 | +<!-- EN-Revision: d6f54016d62904cfd8200604aadd5e3f0d9bad97 Maintainer: lacatoire Status: ready --> |
| 3 | +<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook"> |
| 4 | + <title>Classi anonime</title> |
| 5 | + |
| 6 | + <para> |
| 7 | + Le classi anonime sono utili quando è necessario creare oggetti semplici e utilizzati una sola volta. |
| 8 | + </para> |
| 9 | + |
| 10 | + <informalexample> |
| 11 | + <programlisting role="php"> |
| 12 | +<![CDATA[ |
| 13 | +<?php |
| 14 | +
|
| 15 | +// Utilizzo di una classe esplicita |
| 16 | +class Logger |
| 17 | +{ |
| 18 | + public function log($msg) |
| 19 | + { |
| 20 | + echo $msg; |
| 21 | + } |
| 22 | +} |
| 23 | +
|
| 24 | +$util->setLogger(new Logger()); |
| 25 | +
|
| 26 | +// Utilizzo di una classe anonima |
| 27 | +$util->setLogger(new class { |
| 28 | + public function log($msg) |
| 29 | + { |
| 30 | + echo $msg; |
| 31 | + } |
| 32 | +}); |
| 33 | +]]> |
| 34 | + </programlisting> |
| 35 | + </informalexample> |
| 36 | + |
| 37 | + <para> |
| 38 | + Possono passare argomenti ai loro costruttori, estendere altre classi, |
| 39 | + implementare interfacce e usare trait proprio come una classe normale: |
| 40 | + </para> |
| 41 | + |
| 42 | + <informalexample> |
| 43 | + <programlisting role="php"> |
| 44 | +<![CDATA[ |
| 45 | +<?php |
| 46 | +
|
| 47 | +class SomeClass {} |
| 48 | +interface SomeInterface {} |
| 49 | +trait SomeTrait {} |
| 50 | +
|
| 51 | +var_dump(new class(10) extends SomeClass implements SomeInterface { |
| 52 | + private $num; |
| 53 | +
|
| 54 | + public function __construct($num) |
| 55 | + { |
| 56 | + $this->num = $num; |
| 57 | + } |
| 58 | +
|
| 59 | + use SomeTrait; |
| 60 | +}); |
| 61 | +]]> |
| 62 | + </programlisting> |
| 63 | + &example.outputs; |
| 64 | + <screen> |
| 65 | +<![CDATA[ |
| 66 | +object(class@anonymous)#1 (1) { |
| 67 | + ["Command line code0x104c5b612":"class@anonymous":private]=> |
| 68 | + int(10) |
| 69 | +} |
| 70 | +]]> |
| 71 | + </screen> |
| 72 | + </informalexample> |
| 73 | + |
| 74 | + <para> |
| 75 | + Annidare una classe anonima all'interno di un'altra classe non le dà accesso |
| 76 | + ai metodi o alle proprietà private o protette di quella classe esterna. Per |
| 77 | + utilizzare le proprietà o i metodi protetti della classe esterna, la classe anonima |
| 78 | + può estendere la classe esterna. Per utilizzare le proprietà private della |
| 79 | + classe esterna nella classe anonima, devono essere passate attraverso il suo |
| 80 | + costruttore: |
| 81 | + </para> |
| 82 | + |
| 83 | + <informalexample> |
| 84 | + <programlisting role="php"> |
| 85 | +<![CDATA[ |
| 86 | +<?php |
| 87 | +
|
| 88 | +class Outer |
| 89 | +{ |
| 90 | + private $prop = 1; |
| 91 | + protected $prop2 = 2; |
| 92 | +
|
| 93 | + protected function func1() |
| 94 | + { |
| 95 | + return 3; |
| 96 | + } |
| 97 | +
|
| 98 | + public function func2() |
| 99 | + { |
| 100 | + return new class($this->prop) extends Outer { |
| 101 | + private $prop3; |
| 102 | +
|
| 103 | + public function __construct($prop) |
| 104 | + { |
| 105 | + $this->prop3 = $prop; |
| 106 | + } |
| 107 | +
|
| 108 | + public function func3() |
| 109 | + { |
| 110 | + return $this->prop2 + $this->prop3 + $this->func1(); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +echo (new Outer)->func2()->func3(); |
| 117 | +]]> |
| 118 | + </programlisting> |
| 119 | + &example.outputs; |
| 120 | + <screen> |
| 121 | +<![CDATA[ |
| 122 | +6 |
| 123 | +]]> |
| 124 | + </screen> |
| 125 | + </informalexample> |
| 126 | + |
| 127 | + <para> |
| 128 | + Tutti gli oggetti creati dalla stessa dichiarazione di classe anonima sono istanze |
| 129 | + di quella stessa classe. |
| 130 | + </para> |
| 131 | + |
| 132 | + <informalexample> |
| 133 | + <programlisting role="php"> |
| 134 | +<![CDATA[ |
| 135 | +<?php |
| 136 | +function anonymous_class() |
| 137 | +{ |
| 138 | + return new class {}; |
| 139 | +} |
| 140 | +
|
| 141 | +if (get_class(anonymous_class()) === get_class(anonymous_class())) { |
| 142 | + echo 'same class'; |
| 143 | +} else { |
| 144 | + echo 'different class'; |
| 145 | +}]]> |
| 146 | + </programlisting> |
| 147 | + &example.outputs; |
| 148 | + <screen> |
| 149 | +<![CDATA[ |
| 150 | +same class |
| 151 | +]]> |
| 152 | + </screen> |
| 153 | + </informalexample> |
| 154 | + |
| 155 | + <note> |
| 156 | + <para> |
| 157 | + Le classi anonime ricevono un nome assegnato dal motore, come |
| 158 | + dimostrato nell'esempio seguente. Questo nome deve essere considerato un |
| 159 | + dettaglio implementativo, su cui non si dovrebbe fare affidamento. |
| 160 | + </para> |
| 161 | + <informalexample> |
| 162 | + <programlisting role="php"> |
| 163 | +<![CDATA[ |
| 164 | +<?php |
| 165 | +echo get_class(new class {}); |
| 166 | +]]> |
| 167 | + </programlisting> |
| 168 | + &example.outputs.similar; |
| 169 | + <screen> |
| 170 | +<![CDATA[ |
| 171 | +class@anonymous/in/oNi1A0x7f8636ad2021 |
| 172 | +]]> |
| 173 | + </screen> |
| 174 | + </informalexample> |
| 175 | + </note> |
| 176 | + |
| 177 | + <sect2 xml:id="language.oop5.anonymous.readonly"> |
| 178 | + <title>Classi anonime readonly</title> |
| 179 | + <simpara> |
| 180 | + A partire da PHP 8.3.0, il modificatore <literal>readonly</literal> può |
| 181 | + essere applicato alle classi anonime. |
| 182 | + </simpara> |
| 183 | + <example xml:id="language.oop5.anonymous.readonly.example"> |
| 184 | + <title>Definizione di una classe anonima readonly</title> |
| 185 | + <programlisting role="php"> |
| 186 | + <![CDATA[ |
| 187 | +<?php |
| 188 | +// Utilizzo di una classe anonima |
| 189 | +var_dump(new readonly class('[DEBUG]') { |
| 190 | + public function __construct(private string $prefix) |
| 191 | + { |
| 192 | + } |
| 193 | +
|
| 194 | + public function log($msg) |
| 195 | + { |
| 196 | + echo $this->prefix . ' ' . $msg; |
| 197 | + } |
| 198 | +}); |
| 199 | +]]> |
| 200 | + </programlisting> |
| 201 | + </example> |
| 202 | + </sect2> |
| 203 | +</sect1> |
| 204 | +<!-- Keep this comment at the end of the file |
| 205 | +Local variables: |
| 206 | +mode: sgml |
| 207 | +sgml-omittag:t |
| 208 | +sgml-shorttag:t |
| 209 | +sgml-minimize-attributes:nil |
| 210 | +sgml-always-quote-attributes:t |
| 211 | +sgml-indent-step:1 |
| 212 | +sgml-indent-data:t |
| 213 | +indent-tabs-mode:nil |
| 214 | +sgml-parent-document:nil |
| 215 | +sgml-default-dtd-file:"~/.phpdoc/manual.ced" |
| 216 | +sgml-exposed-tags:nil |
| 217 | +sgml-local-catalogs:nil |
| 218 | +sgml-local-ecat-files:nil |
| 219 | +End: |
| 220 | +vim600: syn=xml fen fdm=syntax fdl=2 si |
| 221 | +vim: et tw=78 syn=sgml |
| 222 | +vi: ts=1 sw=1 |
| 223 | +--> |
0 commit comments