PHP5 Object Model [8] - Access Limit

zhaozj2021-02-16  126

Section VII - Accessing method PHP5 access method allows you to restrict access to class members. This is a new function in PHP5, but there is already an object-oriented language. With access method, you can develop a reliable Object-oriented application, and build reusable object-oriented class libraries. Like C and Java, there are three ways of access: public, private, and protected. For a class member access method, it can be one of them. If you don't have Indicates the way access, the default access method is public. You can also specify an access method for static members, place access to the Static keyword (such as public static) .public static member can be accessed without restrictions. Any code can read and write the public property. You can call a public method from anywhere in the script. In the first few versions of PHP, all methods and properties are public, which makes people feel that the object is exquisite. Array .Private members are only visible inside the class. You cannot change or read its value outside of a class method where a private property is located. Similarly, only the method in the same class can call a private method. The inherited subclass cannot access the Private member in the parent class. To note that any members and classes in the class can access the private member. See Example 6.8, equals method compares two Widgets. == Operator Compare the two objects of the same class, but each object instance has a unique id.equals method only compare Name and Price. Note how the equals method accesses another Widget instance. Java and C are allowed Operation .Listing 6.8 Private Members

Class widget {private $ name; private $ price; private function __construct ($ name, $ price) {$ this-> name = $ name; $ this-> price = floatval ($ price); $ this- > id = uniqid ();} // checks if two widgets are the Same Checks two widgets Are the same public function equals ($ widget) {Return ($ this-> name == $ widget-> name) and ($ this-> price == $ widget-> price));}} $ w1 = new widget ('cog', 5.00); $ w2 = new widget ('Cog', 5.00); $ w3 = new widget ('Gear ', 7.00); // True IF ($ W1-> Equals ($ W2)) {Print ("W1 and W2 Are the Samen");} // false ife ($ w1-> equals ($ w3)) { Print ("W1 and W3 Are the Samen");} // false, == Includes ID in Comparison IF ($ w1 == $ w2) // Nothing, because IDs Different {Print ("W1 and W2 Are The Samen ");}?>

If you are not familiar with object-oriented programming, you may want to know what is the purpose of using Private members. You can recall the idea of ​​packaging and coupling, which is discussed in this chapter. Private members help packaging data. They can Hidden in a class inside without being exposed to the external code. They also help achieve loose coupling. If the code outside the data structure cannot directly access the internal properties, then there is no implicit correlation. Of course, most of the private properties can still be shared by external code. Solution is to use a pair of public methods, one is the GET (value of the property), the other is the set (value of the attribute). The constructor also accepts the initial attribute Value. This makes the communication between members through a narrow and well-defined interface. This also provides opportunities to change the value of the way to the method. Note How to construct a FLOAT in Example 6.8 ( FloadVal ()). Protected members can be accessed by all methods in the same class and inherited classes. Public attributes have a spirit of seasers because they allow subclasses to depend on a specific The attribute is written. PROTECTED method does not bring this concerns. A subclass using the protected method needs to be clearly available. Example 6.9 is improved by example 6.8, including a Widget Sub-class Thing. Note that Widget has a protected method called getName. If the Widget's instance tries to call the protected method will be wrong: $ w1-> getName () generates an error. But the getName method in the subclass Thing can be called This protected method. Of course, for the proven widget :: getName method is protected, this example is too simple. Under the actual situation, use the protected method to rely on the understanding of the internal structure of the object. LiSting 6.9 protected memory; lidget {private $ name; Private $ price; private function __construct ($ name, $ price) {$ this-> name = $ name; $ this-> price = floatval ($ price); $ this-> id = UNIQID (); } // checks if two widgets are the same public fun Ction Equals {Return ($ this-> name == $ widget-> name) and ($ this-> price == $ widget-> price));} protected function getname () {Return This-> name);}} Class Thing Extends Widget {Private $ Color; Public Function SetColor ($ color) {$ this-> color = $ color;} public function getColor () {Return ($ this-> color); } public function getName () {return (Parent :: getname ());}} $ w1 = new widget ('Cog', 5.00); $ w2 = new thing ('cog', 5.00); $ w2-> setcolor ('Yellow'); // true (STILL!) Results Still true if ($ W1-> Equals ($ W2)) {print ("W1 and W2 Are the Samen");} // Print Cog Output Cog Print ($ w2-> getName ());?>

转载请注明原文地址:https://www.9cbs.com/read-8136.html

New Post(0)