mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-18 18:08:54 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@338479 c90b9560-bf6c-de11-be94-00142212c4b1
265 lines
5.8 KiB
XML
265 lines
5.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<sect1 xml:id="language.oop5.visibility" xmlns="http://docbook.org/ns/docbook">
|
|
<title>Visibility</title>
|
|
<para>
|
|
The visibility of a property or method can be defined by prefixing
|
|
the declaration with the keywords <emphasis>public</emphasis>,
|
|
<emphasis>protected</emphasis> or
|
|
<emphasis>private</emphasis>. Class members declared public can be
|
|
accessed everywhere. Members declared protected can be accessed
|
|
only within the class itself and by inherited classes. Members
|
|
declared as private may only be accessed by the class that defines
|
|
the member.
|
|
</para>
|
|
|
|
<sect2 xml:id="language.oop5.visibility-members">
|
|
<title>Property Visibility</title>
|
|
<para>
|
|
Class properties must be defined as public, private, or
|
|
protected. If declared using <emphasis>var</emphasis>,
|
|
the property will be defined as public.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Property declaration</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/**
|
|
* Define MyClass
|
|
*/
|
|
class MyClass
|
|
{
|
|
public $public = 'Public';
|
|
protected $protected = 'Protected';
|
|
private $private = 'Private';
|
|
|
|
function printHello()
|
|
{
|
|
echo $this->public;
|
|
echo $this->protected;
|
|
echo $this->private;
|
|
}
|
|
}
|
|
|
|
$obj = new MyClass();
|
|
echo $obj->public; // Works
|
|
echo $obj->protected; // Fatal Error
|
|
echo $obj->private; // Fatal Error
|
|
$obj->printHello(); // Shows Public, Protected and Private
|
|
|
|
|
|
/**
|
|
* Define MyClass2
|
|
*/
|
|
class MyClass2 extends MyClass
|
|
{
|
|
// We can redeclare the public and protected method, but not private
|
|
protected $protected = 'Protected2';
|
|
|
|
function printHello()
|
|
{
|
|
echo $this->public;
|
|
echo $this->protected;
|
|
echo $this->private;
|
|
}
|
|
}
|
|
|
|
$obj2 = new MyClass2();
|
|
echo $obj2->public; // Works
|
|
echo $obj2->protected; // Fatal Error
|
|
echo $obj2->private; // Undefined
|
|
$obj2->printHello(); // Shows Public, Protected2, Undefined
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<simpara>
|
|
The PHP 4 method of declaring a variable with the
|
|
<emphasis>var</emphasis> keyword is still supported for compatibility
|
|
reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its
|
|
usage would generate an <constant>E_STRICT</constant> warning.
|
|
</simpara>
|
|
</note>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.visiblity-methods">
|
|
<title>Method Visibility</title>
|
|
<para>
|
|
Class methods may be defined as public, private, or
|
|
protected. Methods declared without any explicit visibility
|
|
keyword are defined as public.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Method Declaration</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/**
|
|
* Define MyClass
|
|
*/
|
|
class MyClass
|
|
{
|
|
// Declare a public constructor
|
|
public function __construct() { }
|
|
|
|
// Declare a public method
|
|
public function MyPublic() { }
|
|
|
|
// Declare a protected method
|
|
protected function MyProtected() { }
|
|
|
|
// Declare a private method
|
|
private function MyPrivate() { }
|
|
|
|
// This is public
|
|
function Foo()
|
|
{
|
|
$this->MyPublic();
|
|
$this->MyProtected();
|
|
$this->MyPrivate();
|
|
}
|
|
}
|
|
|
|
$myclass = new MyClass;
|
|
$myclass->MyPublic(); // Works
|
|
$myclass->MyProtected(); // Fatal Error
|
|
$myclass->MyPrivate(); // Fatal Error
|
|
$myclass->Foo(); // Public, Protected and Private work
|
|
|
|
|
|
/**
|
|
* Define MyClass2
|
|
*/
|
|
class MyClass2 extends MyClass
|
|
{
|
|
// This is public
|
|
function Foo2()
|
|
{
|
|
$this->MyPublic();
|
|
$this->MyProtected();
|
|
$this->MyPrivate(); // Fatal Error
|
|
}
|
|
}
|
|
|
|
$myclass2 = new MyClass2;
|
|
$myclass2->MyPublic(); // Works
|
|
$myclass2->Foo2(); // Public and Protected work, not Private
|
|
|
|
class Bar
|
|
{
|
|
public function test() {
|
|
$this->testPrivate();
|
|
$this->testPublic();
|
|
}
|
|
|
|
public function testPublic() {
|
|
echo "Bar::testPublic\n";
|
|
}
|
|
|
|
private function testPrivate() {
|
|
echo "Bar::testPrivate\n";
|
|
}
|
|
}
|
|
|
|
class Foo extends Bar
|
|
{
|
|
public function testPublic() {
|
|
echo "Foo::testPublic\n";
|
|
}
|
|
|
|
private function testPrivate() {
|
|
echo "Foo::testPrivate\n";
|
|
}
|
|
}
|
|
|
|
$myFoo = new Foo();
|
|
$myFoo->test(); // Bar::testPrivate
|
|
// Foo::testPublic
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.visibility-other-objects">
|
|
<title>Visibility from other objects</title>
|
|
<para>
|
|
Objects of the same type will have access to each others private and
|
|
protected members even though they are not the same instances. This is
|
|
because the implementation specific details are already known when inside
|
|
those objects.
|
|
</para>
|
|
<example>
|
|
<title>Accessing private members of the same object type</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class Test
|
|
{
|
|
private $foo;
|
|
|
|
public function __construct($foo)
|
|
{
|
|
$this->foo = $foo;
|
|
}
|
|
|
|
private function bar()
|
|
{
|
|
echo 'Accessed the private method.';
|
|
}
|
|
|
|
public function baz(Test $other)
|
|
{
|
|
// We can change the private property:
|
|
$other->foo = 'hello';
|
|
var_dump($other->foo);
|
|
|
|
// We can also call the private method:
|
|
$other->bar();
|
|
}
|
|
}
|
|
|
|
$test = new Test('test');
|
|
|
|
$test->baz(new Test('other'));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
string(5) "hello"
|
|
Accessed the private method.
|
|
]]>
|
|
</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
|
|
-->
|