mirror of
https://github.com/php/doc-zh.git
synced 2026-03-23 22:52:08 +01:00
150 lines
3.7 KiB
XML
150 lines
3.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 888507ca9ed7a8517edbf2d00a64fcaf5865aa23 Maintainer: jhdxr Status: ready -->
|
||
<!-- CREDITS: Luffy -->
|
||
<sect1 xml:id="language.oop5.static" xmlns="http://docbook.org/ns/docbook">
|
||
<title>静态(static)关键字</title>
|
||
|
||
<tip>
|
||
<simpara>
|
||
本页说明了用 <literal>static</literal> 关键字来定义静态方法和属性。<literal>static</literal>
|
||
也可用于<link linkend="language.variables.scope.static">定义静态变量</link>,
|
||
<link linkend="functions.anonymous-functions.static">静态匿名函数</link>
|
||
以及<link linkend="language.oop5.late-static-bindings">后期静态绑定</link>。参见上述页面了解
|
||
<literal>static</literal> 在其中的用法。
|
||
</simpara>
|
||
</tip>
|
||
|
||
<para>
|
||
声明类属性或方法为静态,就可以不实例化类而直接访问。可以在实例化的类对象中通过静态访问。
|
||
</para>
|
||
|
||
<sect2 xml:id="language.oop5.static.methods">
|
||
<title>静态方法</title>
|
||
|
||
<para>
|
||
由于静态方法不需要通过对象即可调用,所以伪变量 <varname>$this</varname> 在静态方法中不可用。
|
||
</para>
|
||
|
||
<warning>
|
||
<para>
|
||
用静态方式调用一个非静态方法会抛出 <classname>Error</classname>。
|
||
</para>
|
||
<para>
|
||
在 PHP 8.0.0 之前,通过静态方式调用一个非静态方法这种用法已经被废弃,并且会导致一个
|
||
<constant>E_DEPRECATED</constant> 级别的警告。
|
||
</para>
|
||
</warning>
|
||
|
||
<example>
|
||
<title>静态方法示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
class Foo {
|
||
public static function aStaticMethod() {
|
||
// ...
|
||
}
|
||
}
|
||
|
||
Foo::aStaticMethod();
|
||
$classname = 'Foo';
|
||
$classname::aStaticMethod();
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</sect2>
|
||
|
||
<sect2 xml:id="language.oop5.static.properties">
|
||
<title>静态属性</title>
|
||
|
||
<para>
|
||
静态属性使用 <link linkend="language.oop5.paamayim-nekudotayim">范围解析操作符</link>
|
||
( <literal>::</literal> )访问,不能通过对象操作符( <literal>-></literal> )访问。
|
||
</para>
|
||
|
||
<para>
|
||
通过变量来引用一个类是可行的,但这个变量的值不能是一个保留字
|
||
(例如<literal>self</literal>,<literal>parent</literal>和
|
||
<literal>static</literal>)
|
||
</para>
|
||
|
||
<example>
|
||
<title>静态属性示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
class Foo
|
||
{
|
||
public static $my_static = 'foo';
|
||
|
||
public function staticValue() {
|
||
return self::$my_static;
|
||
}
|
||
}
|
||
|
||
class Bar extends Foo
|
||
{
|
||
public function fooStatic() {
|
||
return parent::$my_static;
|
||
}
|
||
}
|
||
|
||
|
||
print Foo::$my_static . "\n";
|
||
|
||
$foo = new Foo();
|
||
print $foo->staticValue() . "\n";
|
||
print $foo->my_static . "\n"; // 未定义的 "属性" my_static
|
||
|
||
print $foo::$my_static . "\n";
|
||
$classname = 'Foo';
|
||
print $classname::$my_static . "\n";
|
||
|
||
print Bar::$my_static . "\n";
|
||
$bar = new Bar();
|
||
print $bar->fooStatic() . "\n";
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.8.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
foo
|
||
foo
|
||
|
||
Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23
|
||
|
||
Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23
|
||
|
||
foo
|
||
foo
|
||
foo
|
||
foo
|
||
]]>
|
||
</screen>
|
||
</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
|
||
-->
|