/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy>
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 8--Access Method
PHP5's access method allows restricting access to class members. This is a new feature in PHP5, but it has long existed 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++ is the same as Java. PHP has three access methods: public, private and protected. The access method of a class member can be one of them. If you do not specify the access method, the default access method is public. You can also use static Members specify an access method, and the access method is placed before the static keyword (such as public static).
Public members can be accessed without restrictions. Any code outside the class can read and write public properties. You can read and write public properties from Call a public method anywhere in the script. In previous versions of PHP, all methods and properties were public, which made objects feel like well-structured arrays.
Private members were only in classes Visible internally. You cannot change or read the value of a private property outside the class method in which it resides. Likewise, only methods in the same class can call a private method. Inherited subclasses cannot access the parent class. private members in.
It should be noted that any member in the class and instances of the class can access private members. See Example 6.8, the equals method compares two widgets. The == operator compares two widgets of the same class. Object, but in this example each object instance has a unique ID. The equals method only compares name and price. Note how the equals method accesses the private property of another Widget instance. Both Java and C allow this operation.
Listing 6.8 Private members
{
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 检查两个widget是否相同
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) //不等,因为ID不同
{
print("w1 and w2 are the same
n");
}
?>
Of course, most private properties can still be shared by external code. The solution is to use a pair of public methods, one is get (get the value of the property), the other is set (set the value of the property). The constructor also accepts The initial value of the property. This allows communication between members to occur through a narrow, well-qualified interface. This also provides the opportunity to change the value passed to the method. Note in Example 6.8 how the constructor forces price to be a float number (floadval()).
Protected (protected) members can be accessed by all methods in the same class and all methods in inherited classes. Public properties violate the spirit of encapsulation because they allow Subclasses rely on a specific attribute to be written. Protected methods do not cause this concern. A subclass that uses a protected method needs to be very clear about the structure of its parent class.
Example 6.9 is derived from Example 6.8 Improved to include a Widget subclass, Thing. Note that Widget now has a protected method called getName. If an instance of Widget attempts to call the protected method an error will occur: $w1->getName() generated an error. But the getName method in the subclass Thing can call this protected method. Of course, this example is too simple to prove that the Widget::getName method is protected. In actual situations, using the protected method depends on understanding the internal structure of the object.
Listing 6.9 Protected members
{
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
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());
}
}
$w1 = new Widget('Cog', 5.00);
$w2 = new Thing('Cog', 5.00);
$w2->setColor('Yellow');
//TRUE (still!) 结果仍然为真
if($w1->equals($w2))
{
print("w1 and w2 are the same
n");
}
//print Cog 输出 Cog
print($w2->getName());
?>
A subclass may change the way the method is accessed by overriding the parent class method. However, there are still some restrictions. If you override a public class member, it must remain public in the subclass. If you override You write a protected member, which can remain protected or become public. Private members are still visible only in the current class. Declaring a member with the same name as a private member of the parent class will simply create a different member in the current class. . Therefore, technically you cannot override a private member.
The Final keyword is another way to restrict access to member methods. Subclasses cannot override methods marked final in the parent class. The Final keyword cannot be used Properties.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools