PHP 5.0 Object model in-depth exploration of how to access _php tutorial

Source: Internet
Author: User
PHP (as the current mainstream development language) 5 access allows restricting access to class members. This is a new feature in PHP 5, which is now a mainstream development language, 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 (which is now the mainstream development language) 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 (which is now the mainstream development language), all methods and properties are public, which makes people feel like an array of ingenious structures.

Private (private) members are visible only inside the class, and 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, and inherited subclasses 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, and both Java and C allow such operations.

Listing 6.8 Private Members

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 n");
}

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

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

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, and 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.

http://www.bkjia.com/PHPjc/508707.html www.bkjia.com true http://www.bkjia.com/PHPjc/508707.html techarticle PHP (as the current mainstream development language) 5 access allows restricting access to class members. This is the new feature in PHP (which is now the mainstream development language) 5, but in many face to ...

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