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.