Access methods and permissions for PHP class members _php tutorial

Source: Internet
Author: User
Tags float number

PHP5 access allows you to restrict access to class members. This is a new feature in PHP5, but it already exists in many object-oriented languages. With access, you can develop a reliable object-oriented application and build a reusable object-oriented class library.

Like C + + and Java, PHP has three ways to access it: public,private and protected. This can be one of the ways that a class member is accessed. If you do not specify the access method, the default access method is public. You can also specify a way to access static members, placing access before the static keyword, such as public static.

Public members can be accessed without restrictions. Any code outside the class can read and write to 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 the object seem like an array of ingenious structures.

Private (private) members are visible only inside the class. You cannot change or read its value outside of the class method where the private property resides. Similarly, only methods in the same class can invoke a private method. Inherited subclasses also cannot access private members in the parent class.

Note that any member of the class and an instance of the class can access the private member. Look at the example 6.8,equals method to compare two widgets. The = = operator Compares two objects of the same class, but each object instance in this example has a unique Id.equals method that compares name and price only. Note how the Equals method accesses the private property of another widget instance. Both Java and C allow such operations.

Class Widget
{
Private $name;
Private $price;
Private $id;

Public function __construct ($name, $price)
{
$this->name = $name;
$this->price = Floatval ($price);
$this->id = Uniqid ();
}
Checks if two widgets is the same check if two widgets are the same
Public function equals ($widget)
{
Return (($this->name = = $widget->name) and
($this->price = = $widget->price));
}
}
$w 1 = new Widget (Cog, 5.00);
$w 2 = new Widget (Cog, 5.00);
$w 3 = new Widget (Gear, 7.00);

TRUE
if ($w 1->equals ($w 2))
{
Print ("W1 and W2 are the same");
}

FALSE
if ($w 1->equals ($w 3))
{
Print ("W1 and W3 are the same");
}

FALSE, = = includes ID in comparison
if ($w 1 = = $w 2)//range because the ID is different
{
Print ("W1 and W2 are the same");
}
? >

If you are not familiar with programming to objects, you may want to know what the purpose of using private members is. You can recall the idea of encapsulation and coupling, which we have discussed at the beginning of this chapter. Private members help encapsulate data. They can be hidden inside a class without being exposed to code outside the class. They also help to achieve loose coupling. If code outside the data structure does not directly access internal properties, then there is no implicit association.

Of course, most private properties can still be shared by external code. The workaround is to use a pair of public methods, one is get (gets the value of the property) and the other is set (the value of the property is set). The constructor also accepts the initial value of the property. This allows for communication between members through a narrow, well-defined interface. This also provides the opportunity to change the value passed to the method. Note In Example 6.8, how the constructor forces the price to be a float number (Floadval ()).

A Protected (protected) member can be accessed by all methods in the same class and all methods in the inherited class. The public attribute is incompatible with the encapsulation spirit, because they allow subclasses to rely on a specific attribute to write. The protected method does not cause this concern. A subclass that uses the protected method needs to be clear about the structure of its parent class.

Note that the widget now has a protected method called GetName. An error occurs if an instance of the widget tries to invoke the protected method: $w 1->getname (). However, the GetName method in subclass thing can call this protected method. Of course, this example is too simple to prove that the Widget::getname method is protected. In practice, the use of the protected method relies on an understanding of the internal structure of the object.


Class Widget
{
Private $name;
Private $price;
Private $id;

Public function __construct ($name, $price)
{
$this->name = $name;
$this->price = Floatval ($price);
$this->id = Uniqid ();
}

Checks if widgets is 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 ()
{
Return ($this->color);
}

Public Function GetName ()
{
Return (Parent::getname ());
}
}

$w 1 = new Widget (Cog, 5.00);
$w 2 = new Thing (Cog, 5.00);
$w 2->setcolor (Yellow);

True (still!) The result is still true
if ($w 1->equals ($w 2))
{
Print ("W1 and W2 are the same");
}

Print COG Output COG
Print ($w 2->getname ());
? >

A subclass may change the way the method is accessed by overwrite the parent class method, although there are still some limitations. If you overwrite a public class member, you must keep public in his subclass. If you overwrite a protected member, it can remain protected or become public. Private members are still visible only in the current class. Declaring a member with the same name as the private member of the parent class will simply create a different member in the current class. Therefore, technically you cannot overwrite a private member.

The final keyword is another way to restrict access to member methods. Subclasses cannot overwrite a method that is identified as final in a parent class. The final keyword cannot be used for attributes.

http://www.bkjia.com/PHPjc/486583.html www.bkjia.com true http://www.bkjia.com/PHPjc/486583.html techarticle PHP5 Access allows you to restrict access to class members. This is a new feature in PHP5, but it already exists in many object-oriented languages. With access to the way to develop a reliable ...

  • 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.