Sample code sharing for OOP6 visibility control in PHP5

Source: Internet
Author: User
As in other languages, it is also restricted by keyword Public,protected,private:

<?php/** * Definition MyClass */class MyClass {public $public = ' publicly ';//Common protected $protected = ' Prote CTED ';//protect private $private = ' private ';//Private Function Printhello () {echo $this-&                 Gt;public;echo "<br/>";                 echo $this->protected;echo "<br/>";         echo $this->private;echo "<br/>"; }} $obj = new MyClass (); Echo $obj->public; Can//echo $obj->protected; Fatal error//echo $obj->private; Fatal error $obj->printhello (); Output public, Protected and Private/** * Definition MyClass2 */class MyClass2 extends MyClass {//For the common and protected methods or members, we can          Re-declare, but private not to protected $protected = ' Protected2 ';                 Rewrite function Printhello () {echo "This is MyClass2:"; echo "<br/>";                 echo $this->public;echo "<br/>";                 echo $this->protected;echo "<br/>"; Echo $this->private;ecHo "<br/>"; Cannot access the private property of the Parent class}} $obj 2 = new MyClass2 (); Echo $obj 2->public; Can//echo $obj 2->private; //echo $obj 2->protected; Not defined Fatal error $obj 2->printhello (); Output public, Protected2, Undefined?>

Of course, with these access controls, we can also implement the same accessible control for the function:

<?php/** * Definition MyClass */class MyClass {///constructor must be public public function construct () {}/ /declares a public method public function Mypublic () {}//Declares protected method protected function myprotected         () {}/Declaration private method Private Function Myprivate () {}//default is Public function Foo ()                 {//class can be accessed directly $this->mypublic ();                 $this->myprotected ();         $this->myprivate (); }} $myclass = new MyClass; $myclass->mypublic (); Can be $myclass->myprotected (); Fatal error $myclass->myprivate ();      Fatal error $myclass->foo ();                 /** * Definition MyClass2 */class MyClass2 extends MyClass {//public function Foo2 () {                 $this->mypublic ();                 $this->myprotected (); $this->myprivate (); Fatal error}} $myclass 2 = new MyClass2; $myclass 2->mypublic (); Can be $myclass 2->foo2 ();     /** * defines bar */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 (); By inheriting the public Method Bar::test () $myFoo->test (); Bar::testprivate//Foo::testpublic?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.