2004-07-10 19:30:37 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2007-06-20 22:25:43 +00:00
|
|
|
<!-- $Revision: 1.12 $ -->
|
|
|
|
<sect1 xml:id="language.oop5.visibility" xmlns="http://docbook.org/ns/docbook">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Visibility</title>
|
|
|
|
<para>
|
2004-07-15 11:03:25 +00:00
|
|
|
The visibility of a property or method can be defined by prefixing the
|
|
|
|
declaration with the keywords: public, protected or private. Public
|
2004-08-13 01:00:48 +00:00
|
|
|
declared items can be accessed everywhere. Protected limits access to
|
2005-08-29 15:51:56 +00:00
|
|
|
inherited and parent classes (and to the class that defines the item).
|
|
|
|
Private limits visibility only to the class that defines the item.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="language.oop5.visiblity-members">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Members Visibility</title>
|
|
|
|
<para>
|
2004-07-15 09:47:21 +00:00
|
|
|
Class members must be defined with public, private, or protected.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
2004-09-26 02:12:31 +00:00
|
|
|
<para>
|
2004-07-10 19:30:37 +00:00
|
|
|
<example>
|
|
|
|
<title>Member declaration</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-09-26 02:12:31 +00:00
|
|
|
/**
|
|
|
|
* Define MyClass
|
|
|
|
*/
|
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
public $public = 'Public';
|
|
|
|
protected $protected = 'Protected';
|
|
|
|
private $private = 'Private';
|
|
|
|
|
|
|
|
function printHello()
|
|
|
|
{
|
|
|
|
echo $this->public;
|
|
|
|
echo $this->protected;
|
|
|
|
echo $this->private;
|
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new MyClass();
|
2004-09-26 02:12:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
$obj2 = new MyClass2();
|
2004-09-26 02:12:31 +00:00
|
|
|
echo $obj->public; // Works
|
|
|
|
echo $obj2->private; // Undefined
|
|
|
|
echo $obj2->protected; // Fatal Error
|
|
|
|
$obj2->printHello(); // Shows Public, Protected2, not Private
|
2004-07-10 19:30:37 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2004-09-26 02:12:31 +00:00
|
|
|
</para>
|
2004-07-10 19:30:37 +00:00
|
|
|
<note>
|
|
|
|
<simpara>
|
2004-10-13 11:19:09 +00:00
|
|
|
The PHP 4 method of declaring a variable with the
|
2006-08-12 16:44:18 +00:00
|
|
|
<emphasis>var</emphasis> keyword is still supported for compatibility
|
2006-08-12 17:24:36 +00:00
|
|
|
reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its
|
2006-08-12 16:44:18 +00:00
|
|
|
usage would generate an <constant>E_STRICT</constant> warning.
|
2004-07-10 19:30:37 +00:00
|
|
|
</simpara>
|
|
|
|
</note>
|
|
|
|
</sect2>
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="language.oop5.visiblity-methods">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Method Visibility</title>
|
|
|
|
<para>
|
2004-09-26 02:12:31 +00:00
|
|
|
Class methods must be defined with public, private, or protected. Methods
|
|
|
|
without any declaration are defined as public.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
2004-09-26 02:12:31 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Method Declaration</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Define MyClass
|
|
|
|
*/
|
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
// Contructors must be public
|
2004-10-11 15:09:41 +00:00
|
|
|
public function __construct() { }
|
2004-09-26 02:12:31 +00:00
|
|
|
|
|
|
|
// Declare a public method
|
2004-10-11 15:09:41 +00:00
|
|
|
public function MyPublic() { }
|
2004-09-26 02:12:31 +00:00
|
|
|
|
|
|
|
// Declare a protected method
|
2004-10-11 15:09:41 +00:00
|
|
|
protected function MyProtected() { }
|
2004-09-26 02:12:31 +00:00
|
|
|
|
|
|
|
// Declare a private method
|
2004-10-11 15:09:41 +00:00
|
|
|
private function MyPrivate() { }
|
2004-09-26 02:12:31 +00:00
|
|
|
|
|
|
|
// This is public
|
2004-10-11 15:09:41 +00:00
|
|
|
function Foo()
|
2004-09-26 02:12:31 +00:00
|
|
|
{
|
2004-10-11 15:09:41 +00:00
|
|
|
$this->MyPublic();
|
|
|
|
$this->MyProtected();
|
|
|
|
$this->MyPrivate();
|
2004-09-26 02:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2004-09-26 02:12:31 +00:00
|
|
|
$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
|
2004-10-11 15:09:41 +00:00
|
|
|
function Foo2()
|
2004-09-26 02:12:31 +00:00
|
|
|
{
|
2004-10-11 15:09:41 +00:00
|
|
|
$this->MyPublic();
|
|
|
|
$this->MyProtected();
|
|
|
|
$this->MyPrivate(); // Fatal Error
|
2004-09-26 02:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2004-09-26 02:12:31 +00:00
|
|
|
$myclass2 = new MyClass2;
|
|
|
|
$myclass2->MyPublic(); // Works
|
|
|
|
$myclass2->Foo2(); // Public and Protected work, not Private
|
2007-04-27 14:39:54 +00:00
|
|
|
|
|
|
|
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
|
2004-09-26 02:12:31 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</sect2>
|
2004-07-10 19:30:37 +00:00
|
|
|
</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
|
|
|
|
-->
|