Home > Article > Backend Development > Access methods for in-depth exploration of PHP 5.0 object model_PHP tutorial
php (as the current mainstream development language) 5’s access method allows restricting access to class members. This is a new feature in php(as the current mainstream development language)5, but it has already existed in many object-oriented languages. With access methods, you can develop a reliable object-oriented application and build a reusable object-oriented class library.
Like C++ and Java, php (as the current mainstream development language) has three access methods: public, private and protected. For the access method of a class member, it can be one of them 1. If you do not specify the access method, the default access method is public. You can also specify an access method for static members and put the access method before the static keyword (such as public static).
Public members can be accessed without restriction. Any code outside the class can read and write public properties. You can call a public method from anywhere in the script. In the first few versions of php(as the current mainstream development language), all methods and properties were public, which made objects feel like well-structured arrays.
Private members are only visible inside the class. You cannot change or read the value of a private attribute outside the class method where it is located. Similarly, only methods in the same class can call a private method, and inherited subclasses cannot access private members in the parent class.
It should be noted that any member of the class and instances of the class can access private members. Looking at Example 6.8, the equals method compares two widgets. The == operator compares two objects of the same class, but in this example each object instance has a unique ID. The equals method only compares name and price. Notice how the equals method accesses a private property of another Widget instance. Both Java and C allow this.
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 are the same Check if 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 n");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the same n");
}
//FALSE, == includes id in comparison
if($w1 == $w2) / / Not equal, because the IDs are different
{
print("w1 and w2 are the same n");
}
?>
If you are not familiar with object-oriented programming , you may wonder what the purpose of using private members is. You may recall the ideas of encapsulation and coupling, which we discussed at the beginning of this chapter. Private members help encapsulate data. They can be hidden within a class from being accessed by code outside the class. They also help achieve loose coupling. If code outside the data structure cannot directly access the internal properties, then There is no implicit correlation.