mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00
Reworked Members visibility
Added Method visibility git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169243 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
c857b14deb
commit
23b60ef80e
1 changed files with 103 additions and 42 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.4 $ -->
|
||||
<!-- $Revision: 1.5 $ -->
|
||||
<sect1 id="language.oop5.visibility">
|
||||
<title>Visibility</title>
|
||||
<para>
|
||||
|
@ -14,62 +14,63 @@
|
|||
<para>
|
||||
Class members must be defined with public, private, or protected.
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
<title>Member declaration</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
/**
|
||||
* Define MyClass
|
||||
*/
|
||||
class MyClass
|
||||
{
|
||||
public $public = 'Public';
|
||||
protected $protected = 'Protected';
|
||||
private $private = 'Private';
|
||||
|
||||
class MyClass {
|
||||
public $public = "MyClass::public!\n";
|
||||
protected $protected = "MyClass::Protected!\n";
|
||||
protected $protected2 = "MyClass::Protected2!\n";
|
||||
private $private = "MyClass::private!\n";
|
||||
|
||||
function printHello() {
|
||||
print "MyClass::printHello() " . $this->private;
|
||||
print "MyClass::printHello() " . $this->protected;
|
||||
print "MyClass::printHello() " . $this->protected2;
|
||||
}
|
||||
}
|
||||
|
||||
class MyClass2 extends MyClass {
|
||||
protected $protected = "MyClass2::protected!\n";
|
||||
|
||||
function printHello() {
|
||||
|
||||
MyClass::printHello();
|
||||
|
||||
print "MyClass2::printHello() " . $this->public;
|
||||
print "MyClass2::printHello() " . $this->protected;
|
||||
print "MyClass2::printHello() " . $this->protected2;
|
||||
|
||||
/* Will result in a Fatal Error: */
|
||||
//print "MyClass2::printHello() " . $this->private; /* Fatal Error */
|
||||
|
||||
}
|
||||
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
|
||||
|
||||
print "Main:: " . $obj->public;
|
||||
//print $obj->private; /* Fatal Error */
|
||||
//print $obj->protected; /* Fatal Error */
|
||||
//print $obj->protected2; /* Fatal Error */
|
||||
|
||||
$obj->printHello(); /* Should print */
|
||||
/**
|
||||
* 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();
|
||||
print "Main:: " . $obj2->private; /* Undefined */
|
||||
echo $obj->public; // Works
|
||||
echo $obj2->private; // Undefined
|
||||
echo $obj2->protected; // Fatal Error
|
||||
$obj2->printHello(); // Shows Public, Protected2, not Private
|
||||
|
||||
//print $obj2->protected; /* Fatal Error */
|
||||
//print $obj2->protected2; /* Fatal Error */
|
||||
|
||||
$obj2->printHello();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<simpara>
|
||||
The use PHP 4 use of declaring a variable with the keyword 'var' is
|
||||
|
@ -82,11 +83,71 @@ $obj2->printHello();
|
|||
<sect2 id="language.oop5.visiblity-methods">
|
||||
<title>Method Visibility</title>
|
||||
<para>
|
||||
.
|
||||
Class methods must be defined with public, private, or protected. Methods
|
||||
without any declaration are defined as public.
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
<title>Method Declaration</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
/**
|
||||
* Define MyClass
|
||||
*/
|
||||
class MyClass
|
||||
{
|
||||
// Contructors must be public
|
||||
public __construct() { }
|
||||
|
||||
// Declare a public method
|
||||
public MyPublic() { }
|
||||
|
||||
// Declare a protected method
|
||||
protected MyProtected() { }
|
||||
|
||||
// Declare a private method
|
||||
private MyPrivate() { }
|
||||
|
||||
// This is public
|
||||
Foo()
|
||||
{
|
||||
$this->public();
|
||||
$this->protected();
|
||||
$this->private();
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
Foo2()
|
||||
{
|
||||
$this->public();
|
||||
$this->protected();
|
||||
$this->private();
|
||||
}
|
||||
}
|
||||
|
||||
$myclass2 = new MyClass2;
|
||||
$myclass2->MyPublic(); // Works
|
||||
$myclass2->Foo2(); // Public and Protected work, not Private
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
|
||||
</sect1>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
|
|
Loading…
Reference in a new issue