aidan Sat Sep 25 22:12:32 2004 EDT
Modified files: /phpdoc/en/language/oop5 visibility.xml Log: Reworked Members visibility Added Method visibility http://cvs.php.net/diff.php/phpdoc/en/language/oop5/visibility.xml?r1=1.4&r2=1.5&ty=u Index: phpdoc/en/language/oop5/visibility.xml diff -u phpdoc/en/language/oop5/visibility.xml:1.4 phpdoc/en/language/oop5/visibility.xml:1.5 --- phpdoc/en/language/oop5/visibility.xml:1.4 Thu Aug 12 21:00:44 2004 +++ phpdoc/en/language/oop5/visibility.xml Sat Sep 25 22:12:31 2004 @@ -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 - -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 */ - - } +/** + * 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(); - -print "Main:: " . $obj->public; -//print $obj->private; /* Fatal Error */ -//print $obj->protected; /* Fatal Error */ -//print $obj->protected2; /* Fatal Error */ - -$obj->printHello(); /* Should print */ +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(); -print "Main:: " . $obj2->private; /* Undefined */ - -//print $obj2->protected; /* Fatal Error */ -//print $obj2->protected2; /* Fatal Error */ +echo $obj->public; // Works +echo $obj2->private; // Undefined +echo $obj2->protected; // Fatal Error +$obj2->printHello(); // Shows Public, Protected2, not Private -$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 @@ <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> - </sect2> + <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