mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
The content for the static keyword.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@163427 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
9d0a1e7664
commit
d62a61a076
1 changed files with 72 additions and 3 deletions
|
@ -1,11 +1,80 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.1 $ -->
|
||||
<!-- $Revision: 1.2 $ -->
|
||||
<sect1 id="language.oop5.static">
|
||||
<title>Static</title>
|
||||
<title>Static Keyword</title>
|
||||
|
||||
<para>
|
||||
.
|
||||
Declaring class members or methods as static, makes them callable
|
||||
from outside the object context. A member or method declared
|
||||
with static can not be accessed with a variable that is an instance
|
||||
of the object and cannot be re-defined in an extending class.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The static declaration must be after the visibilty declaration. For
|
||||
compatibility with PHP 4, if no <link
|
||||
linkend="language.oop5.visibility">visibility</link>
|
||||
declaration is used, then the member or method will be treated
|
||||
as if it was declared as <literal>public static</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Because static methods are callable without an instance of
|
||||
the object created, the pseudo variable <varname>$this</varname> is
|
||||
not available inside the method declared as static.
|
||||
</para>
|
||||
|
||||
<example>
|
||||
<title>Static member example</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"; // Undefined my_static
|
||||
|
||||
print Bar::$my_static . "\n";
|
||||
$bar = new Bar();
|
||||
print $bar->fooStatic() . "\n";
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
<example>
|
||||
<title>Static method example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class Foo {
|
||||
public static function aStaticMethod() {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
Foo::aStaticMethod();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
</sect1>
|
||||
|
||||
|
|
Loading…
Reference in a new issue