1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-23 22:52:08 +01:00
Files
archived-doc-zh/language/oop5/anonymous.xml
2025-11-08 11:00:48 +08:00

219 lines
4.4 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: d6f54016d62904cfd8200604aadd5e3f0d9bad97 Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook">
<title>匿名类</title>
<para>
匿名类很有用,可以创建一次性的简单对象。
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// 使用显性类
class Logger
{
public function log($msg)
{
echo $msg;
}
}
$util->setLogger(new Logger());
// 使用匿名类
$util->setLogger(new class {
public function log($msg)
{
echo $msg;
}
});
]]>
</programlisting>
</informalexample>
<para>
可以传递参数到匿名类的构造器也可以继承其他类、实现接口implement interface以及像其他普通的类一样使用 trait
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class SomeClass {}
interface SomeInterface {}
trait SomeTrait {}
var_dump(new class(10) extends SomeClass implements SomeInterface {
private $num;
public function __construct($num)
{
$this->num = $num;
}
use SomeTrait;
});
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(class@anonymous)#1 (1) {
["Command line code0x104c5b612":"class@anonymous":private]=>
int(10)
}
]]>
</screen>
</informalexample>
<para>
匿名类被嵌套进普通 Class 后不能访问这个外部类Outer class的 private私有、protected受保护方法或者属性。
为了访问外部类Outer classprotected 属性或方法,匿名类可以 extend扩展此外部类。
为了使用外部类Outer class的 private 属性,必须通过构造器传进来:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class Outer
{
private $prop = 1;
protected $prop2 = 2;
protected function func1()
{
return 3;
}
public function func2()
{
return new class($this->prop) extends Outer {
private $prop3;
public function __construct($prop)
{
$this->prop3 = $prop;
}
public function func3()
{
return $this->prop2 + $this->prop3 + $this->func1();
}
};
}
}
echo (new Outer)->func2()->func3();
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
6
]]>
</screen>
</informalexample>
<para>
声明的同一个匿名类,所创建的对象都是这个类的实例。
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function anonymous_class()
{
return new class {};
}
if (get_class(anonymous_class()) === get_class(anonymous_class())) {
echo 'same class';
} else {
echo 'different class';
}]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
same class
]]>
</screen>
</informalexample>
<note>
<para>
注意,匿名类的名称是通过引擎赋予的,如下例所示。
由于实现的细节,不应该去依赖这个类名。
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo get_class(new class {});
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
class@anonymous/in/oNi1A0x7f8636ad2021
]]>
</screen>
</informalexample>
</note>
<sect2 xml:id="language.oop5.anonymous.readonly">
<title>只读匿名类</title>
<simpara>
自 PHP 8.3.0 起,<literal>readonly</literal> 修饰符可应用于匿名类。
</simpara>
<example xml:id="language.oop5.anonymous.readonly.example">
<title>定义 readonly 匿名类</title>
<programlisting role="php">
<![CDATA[
<?php
// 使用匿名类
var_dump(new readonly class('[DEBUG]') {
public function __construct(private string $prefix)
{
}
public function log($msg)
{
echo $this->prefix . ' ' . $msg;
}
});
]]>
</programlisting>
</example>
</sect2>
</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
-->