mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00
Cleaned up and made more reference-like.
Split docs for properties out into its own section. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@288217 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
debd8760a6
commit
813758ad4f
3 changed files with 206 additions and 106 deletions
|
@ -30,12 +30,13 @@
|
|||
</sect1>
|
||||
|
||||
&language.oop5.basic;
|
||||
&language.oop5.properties;
|
||||
&language.oop5.constants;
|
||||
&language.oop5.autoload;
|
||||
&language.oop5.decon;
|
||||
&language.oop5.visibility;
|
||||
&language.oop5.paamayim-nekudotayim;
|
||||
&language.oop5.static;
|
||||
&language.oop5.constants;
|
||||
&language.oop5.abstract;
|
||||
&language.oop5.interfaces;
|
||||
&language.oop5.overloading;
|
||||
|
|
|
@ -9,9 +9,43 @@
|
|||
<para>
|
||||
Every class definition begins with the
|
||||
keyword <literal>class</literal>, followed by a class name,
|
||||
followed by a pair of curly braces which enclose the definition of
|
||||
the class's members and methods. The
|
||||
pseudo-variable <varname>$this</varname> is available when a
|
||||
followed by a pair of curly braces which enclose the definitions
|
||||
of the class's properties and methods.
|
||||
</para>
|
||||
<para>
|
||||
The class name can be any valid label which is a not a
|
||||
PHP <link linkend="reserved">reserved word</link>. A valid class
|
||||
name starts with a letter or underscore, followed by any number of
|
||||
letters, numbers, or underscores. As a regular expression, it
|
||||
would be expressed thus:
|
||||
<literal>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</literal>.
|
||||
</para>
|
||||
<para>
|
||||
A class may contain its
|
||||
own <link linkend="language.oop5.constants">constants</link>, <link linkend="language.oop5.properties">variables</link>
|
||||
(called "properties"), and functions (called "methods").
|
||||
</para>
|
||||
<example>
|
||||
<title>Simple Class definition</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class SimpleClass
|
||||
{
|
||||
// property declaration
|
||||
public $var = 'a default value';
|
||||
|
||||
// method declaration
|
||||
public function displayVar() {
|
||||
echo $this->var;
|
||||
}
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
<para>
|
||||
The pseudo-variable <varname>$this</varname> is available when a
|
||||
method is called from within an object
|
||||
context. <varname>$this</varname> is a reference to the calling
|
||||
object (usually the object to which the method belongs, but
|
||||
|
@ -20,19 +54,8 @@
|
|||
of a secondary object).
|
||||
</para>
|
||||
<para>
|
||||
The class name can be any name which is a not a
|
||||
PHP <link linkend="reserved">reserved word</link> and which is a
|
||||
valid label--that is, it must conform to
|
||||
the <link linkend="language.variables.basics">PHP rules for
|
||||
variable naming</link> except that the class name has no preceding
|
||||
dollar sign.
|
||||
</para>
|
||||
<para>
|
||||
Here are some basic examples of class definition, object
|
||||
construction, and use of the <varname>$this</varname>
|
||||
pseudo-variable:
|
||||
<example>
|
||||
<title><varname>$this</varname> variable in object-oriented language</title>
|
||||
<title>Some examples of the <varname>$this</varname> pseudo-variable</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
@ -54,15 +77,20 @@ class B
|
|||
{
|
||||
function bar()
|
||||
{
|
||||
// Note: the next line will issue a warning if E_STRICT is enabled.
|
||||
A::foo();
|
||||
}
|
||||
}
|
||||
|
||||
$a = new A();
|
||||
$a->foo();
|
||||
|
||||
// Note: the next line will issue a warning if E_STRICT is enabled.
|
||||
A::foo();
|
||||
$b = new B();
|
||||
$b->bar();
|
||||
|
||||
// Note: the next line will issue a warning if E_STRICT is enabled.
|
||||
B::bar();
|
||||
?>
|
||||
]]>
|
||||
|
@ -78,88 +106,6 @@ $this is not defined.
|
|||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
<example>
|
||||
<title>Simple Class definition</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class SimpleClass
|
||||
{
|
||||
// member declaration
|
||||
public $var = 'a default value';
|
||||
|
||||
// method declaration
|
||||
public function displayVar() {
|
||||
echo $this->var;
|
||||
}
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
<para>
|
||||
The default value must be a constant expression, not (for example) a
|
||||
variable, a class member or a function call.
|
||||
<example>
|
||||
<title>Class members' default value</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class SimpleClass
|
||||
{
|
||||
// invalid member declarations:
|
||||
public $var1 = 'hello '.'world';
|
||||
public $var2 = <<<EOD
|
||||
hello world
|
||||
EOD;
|
||||
public $var3 = 1+2;
|
||||
public $var4 = self::myStaticMethod();
|
||||
public $var5 = $myVar;
|
||||
|
||||
// valid member declarations:
|
||||
public $var6 = myConstant;
|
||||
public $var7 = self::classConstant;
|
||||
public $var8 = array(true, false);
|
||||
|
||||
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
There are some nice functions to handle classes and objects. You might want
|
||||
to take a look at the <link linkend="ref.classobj">Class/Object
|
||||
Functions</link>.
|
||||
</para>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
Unlike heredocs, nowdocs can be used in any static data context.
|
||||
<example>
|
||||
<title>Static data example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class foo {
|
||||
// As of PHP 5.3.0
|
||||
public $bar = <<<'EOT'
|
||||
bar
|
||||
EOT;
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
Nowdoc support was added in PHP 5.3.0.
|
||||
</para>
|
||||
</note>
|
||||
</sect2>
|
||||
|
||||
<sect2 xml:id="language.oop5.basic.new">
|
||||
|
@ -233,16 +179,19 @@ object(SimpleClass)#1 (1) {
|
|||
<sect2 xml:id="language.oop5.basic.extends">
|
||||
<title>extends</title>
|
||||
<para>
|
||||
A class can inherit methods and members of another class by using the
|
||||
extends keyword in the declaration. It is not possible to extend multiple
|
||||
classes, a class can only inherit one base class.
|
||||
A class can inherit the methods and properties of another class by
|
||||
using the keyword <literal>extends</literal> in the class
|
||||
declaration. It is not possible to extend multiple classes; a
|
||||
class can only inherit from one base class.
|
||||
</para>
|
||||
<para>
|
||||
The inherited methods and members can be overridden, unless the parent
|
||||
class has defined a method as <link linkend="language.oop5.final">final</link>,
|
||||
by redeclaring them with the same name defined in the parent class.
|
||||
It is possible to access the overridden methods or static members by
|
||||
referencing them with <link linkend="language.oop5.paamayim-nekudotayim">parent::</link>
|
||||
The inherited methods and properties can be overridden by
|
||||
redeclaring them with the same name defined in the parent
|
||||
class. However, if the parent class has defined a method
|
||||
as <link linkend="language.oop5.final">final</link>, that method
|
||||
may not be overridden. It is possible to access the overridden
|
||||
methods or static properties by referencing them
|
||||
with <link linkend="language.oop5.paamayim-nekudotayim">parent::</link>
|
||||
</para>
|
||||
<example>
|
||||
<title>Simple Class Inheritance</title>
|
||||
|
|
150
language/oop5/properties.xml
Normal file
150
language/oop5/properties.xml
Normal file
|
@ -0,0 +1,150 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 283798 $ -->
|
||||
<sect1 xml:id="language.oop5.properties" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Properties</title>
|
||||
|
||||
<para>
|
||||
Class member variables are called "properties". You may also see
|
||||
them referred to using other terms such as "attributes" or
|
||||
"fields", but for the purposes of this reference we will use
|
||||
"properties". They are defined by using one of the
|
||||
keywords <literal>public</literal>, <literal>protected</literal>,
|
||||
or <literal>private</literal>, followed by a normal variable
|
||||
declaration. This declaration may include an initialization, but
|
||||
this initialization must be a constant value--that is, it must be
|
||||
able to be evaluated at compile time and must not depend on
|
||||
run-time information in order to be evaluated.
|
||||
</para>
|
||||
<para>
|
||||
See <xref linkend="language.oop5.visibility" /> for more
|
||||
information on the meanings
|
||||
of <literal>public</literal>, <literal>protected</literal>,
|
||||
and <literal>private</literal>.
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
In order to maintain backward compatibility with PHP 4, PHP 5 will
|
||||
still accept the use of the keyword <literal>var</literal> in
|
||||
property declarations instead of (or in addition
|
||||
to) <literal>public</literal>, <literal>protected</literal>,
|
||||
or <literal>private</literal>. However, <literal>var</literal> is
|
||||
no longer required. In versions of PHP from 5.0 to 5.1.3, the use
|
||||
of <literal>var</literal> was considered deprecated and would
|
||||
issue an <constant>E_STRICT</constant> warning, but since PHP
|
||||
5.1.3 it is no longer deprecated and does not issue the warning.
|
||||
</para>
|
||||
<para>
|
||||
If you declare a property using <literal>var</literal> instead of
|
||||
one of <literal>public</literal>, <literal>protected</literal>,
|
||||
or <literal>private</literal>, then PHP 5 will treat the property
|
||||
as if it had been declared as <literal>public</literal>.
|
||||
</para>
|
||||
</note>
|
||||
<para>
|
||||
Within class methods the properties, constants, and methods may be
|
||||
accessed by using the form <varname>$this->property</varname>
|
||||
(where <literal>property</literal> is the name of the property)
|
||||
unless the access is to a static property within the context of a
|
||||
static class method, in which case it is accessed using the
|
||||
form <varname>self::$property</varname>. See <link linkend="language.oop5.static">Static
|
||||
Keyword</link> for more information.
|
||||
</para>
|
||||
<para>
|
||||
The pseudo-variable <varname>$this</varname> is available inside
|
||||
any class method when that method is called from within an object
|
||||
context. <varname>$this</varname> is a reference to the calling
|
||||
object (usually the object to which the method belongs, but
|
||||
possibly another object, if the method is called
|
||||
<link linkend="language.oop5.static">statically</link> from the context
|
||||
of a secondary object).
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<example>
|
||||
<title>property declarations</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class SimpleClass
|
||||
{
|
||||
// invalid member declarations:
|
||||
public $var1 = 'hello ' . 'world';
|
||||
public $var2 = <<<EOD
|
||||
hello world
|
||||
EOD;
|
||||
public $var3 = 1+2;
|
||||
public $var4 = self::myStaticMethod();
|
||||
public $var5 = $myVar;
|
||||
|
||||
// valid member declarations:
|
||||
public $var6 = myConstant;
|
||||
public $var7 = array(true, false);
|
||||
|
||||
// This is allowed only in PHP 5.3.0 and later.
|
||||
public $var8 = <<<'EOD'
|
||||
hello world
|
||||
EOD;
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
|
||||
<note>
|
||||
<para>
|
||||
There are some nice functions to handle classes and objects. You
|
||||
might want to take a look at
|
||||
the <link linkend="ref.classobj">Class/Object Functions</link>.
|
||||
</para>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
Unlike
|
||||
<link linkend="language.types.string.syntax.heredoc">heredocs</link>,
|
||||
<link linkend="language.types.string.syntax.nowdoc">nowdocs</link>
|
||||
can be used in any static data context, including property
|
||||
declarations.
|
||||
<example>
|
||||
<title>Example of using a nowdoc to initialize a property</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class foo {
|
||||
// As of PHP 5.3.0
|
||||
public $bar = <<<'EOT'
|
||||
bar
|
||||
EOT;
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
Nowdoc support was added in PHP 5.3.0.
|
||||
</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:"../../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…
Reference in a new issue