Php access control

Source: Internet
Author: User
Php access control Access control is implemented by the public, protected, and private keywords. It is defined as a public class member that can be accessed anywhere. A protected class member can be accessed by itself, its subclass, and its parent class. A private class member can only be accessed by the class it defines.

Class attributes must be defined as public, protected, or private. To be compatible with versions earlier than PHP5, if var is used, it is considered public.

Class Car {$ speed = 10; // The error attribute must define the public $ name of the access control; // define the common attribute}

Classes can be defined as public, private, or protected. If these keywords are not set, the method is public by default.

Class Car {? // The default value is the total function turnLeft (){}}

If the constructor is defined as a private method, the object cannot be directly instantiated. in this case, the static method is generally used to instantiate the object. in the design mode, this method is often used to control the creation of the object, for example, in Singleton mode, only one globally unique object is allowed.

Class Car {private function _ construct () {echo 'object create';} private static $ _ object = null; public static function getInstance () {if (empty (self :: $ _ object) {self ::$ _ object = new Car (); // internal methods can call private methods, so you can create an object here} return self :: $ _ object ;}/// $ car = new Car (); // The object $ car = Car: getInstance () cannot be directly instantiated here (); // Obtain an instance through static methods

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.