Section 8 - Accessing - Classes and Objects in PHP5 [8]

zhaozj2021-02-16  57

Section 8 - Accessing - Classes and Objects in PHP5 [8]

Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Section 8 - Accessing method PHP5 access method Allow access to class members. This is a new function in PHP5, but it has already existed in many object-oriented languages. With access, it can develop a reliable object-oriented application, and build reuse Object class library. Like C and Java, PHP has three access methods: public, private and protected. For a class member access method, it can be one of them. If you don't specify access, the default access is public. You can also specify an access method for static members, place access methods before static keywords (such as public static) .Public members can be accessed without restrictions. Any code outside the class can read and write public properties. You You can call a public method from the script. In the first few versions of PHP, all methods and properties are public, which makes people feel that the object is like a structured array. Private (private) member is only class It is visible inside. 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. Inherited subclasses cannot access the parent class Private member. Be careful, any member and class of any members and classes can access the Private member. See Example 6.8, equals method compares two Widgets. == Operator compares two objects of the same class, but this Each object instance has a unique id.equals method only compares Name and Price. Note how the equals method accesses another Widget instance. Java and C are allowed to do this .li sting 6.8 Private members name = $ name; $ this-> price = floatval ( $ price); $ this-> id = uniqid ();} // checks if two widgets are the Same Check 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 Same
);} // False IF $ w1->

Equals ($ w3)) {print ("w1 and w3 areate
);} // false, == Includes ID in Comparison IF ($ w1 == $ w2) // Nothing, because id Different {Print ("W1 and W2 Are the Same
);}?> 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 packaging and coupling I have discussed this at the beginning of this chapter. Private members help packages. They can hide the interior of a class without being exposed to the external code. At the same time they also help to achieve loose coupling. If the data structure The code 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 Get (get attribute) Value), the other is the set (set the value of the attribute). The constructor also accepts the initial value of the attribute. This makes the communication between the members through a narrow, well-defined interface. This also provides a change to the method. The chance of value. Note that in Example 6.8, the constructor enables the Price to become a float number (FloadVal ()). Protected member can be all the methods in the same class and inherited class all Method Access to. Public attributes have a spirit of violations because they allow subclasses to demonstrate a specific attribute to write. PROTECTED methods do not bring this concern. A subclass using protected methods need to be clear. The structure of the parent class. Example 6.9 is improved by Example 6.8 and contains a widget subclass Thing. Note that widget has a protected method called getName. If widget is actually trying to call the protected method, it will be wrong: $ W1-> getName () produces an error. However, the getName method in the subclass strokes can call this protected method. Of course, for the proven widget :: getName method is protected, this example is too simple. Under the actual situation, use protected method To rely on the understanding of the internal structure of the object .Listing 6.9 protected mebers name = $ name; $ this-> price = floatval ($ price); $ this-> id = uniqid ();} // checks if two widgets Are The Same Public Function Equals ($ Widget) {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 ()} public function getColor ()} ($ this > color);} public function getName () {return (parent :: getname ());}} $ w1 = new widget ('Cog', 5.00); $ w2 = new string ('Cog', 5.00);

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

New Post(0)