PHP: Access control for members in a class

Source: Internet
Author: User
Access control

Access control for a property or method is achieved by adding the keyword public, protected, or private earlier.

A class member defined by public can be accessed from anywhere;

A class member defined by protected can be accessed by the subclass and parent of its class (of course, the class where the member resides), while the class member defined by private is accessible only to the class in which it resides.

The following 3 types of access modifiers are supported in PHP5.

(1) public. This modifier is default, and if an access modifier is not specified for the property or method, it will

is public. A common property or method can be accessed both inside and outside the class.

(2) Private. This modifier indicates that a tagged property or method can be accessed only within the class. Can not be inherited!

(3) protected. This modifier indicates that a tagged property or method can be accessed only within the class. can be further

Private the same class can be accessed, subclasses and external classes are inaccessible;

Protected the same class and subclass can be accessed, the external class cannot be accessed.

Access control for class members

Class members must be defined using the keyword public, protected, or private

<?phpclass 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; This line can be performed normally echo $obj->protected; This guild produces a fatal error. Echo $obj->private; This line will also produce a fatal error $obj->printhello (); Output public, Protected, and Privateclass MyClass2 extends myclass{//can be redefined for public and Protected, but private and not Protec    Ted $protected = ' Protected2 ';        function Printhello () {echo $this->public;        Echo $this->protected;    Echo $this->private; }} $obj 2 = new MyClass2 (); Echo $obj->public; This line can be performed normally echo $obj 2->private; Privateecho $obj 2->protected; Not defined This guild produces a fatal error $obj2->printhello ();        Outputs public, PROTECTED2, but does not output privateclass 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?>

Access control of the method

Methods in a class must be defined using the keyword public, protected, or private. If these keywords are not set, the method is set to the default public.

<?phpclass myclass{//constructor must be public public function construct () {}// Declares a public method public function Mypublic () {}//declares a protected method protected function myprotected () {}//Sound Ming a Private method Private function Myprivate () {}//This method is also public function Foo () {$this->mypublic        ();        $this->myprotected ();    $this->myprivate (); }} $myclass = new MyClass; $myclass->mypublic (); This line can be executed normally $myclass->myprotected (); This guild produces a fatal error $myclass->myprivate (); This guild produces a fatal error $myclass->foo (); Public, Protected, and Private are called the class MyClass2 extends myclass{//This is public function Foo2 () {$th        Is->mypublic ();        $this->myprotected (); $this->myprivate (); This guild produces a fatal error}} $myclass 2 = new MyClass2; $myclass 2->mypublic (); This line can be executed normally $myclass2->foo2 (); Both public and Protected are called, but Private is not called. 
Related Article

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.