Understand visibility concepts in object-oriented PHP

I have recently written a lot about PHP’s object-oriented programming (OOP) principles, including magic methods. OOP facilitates code encapsulation: separate functions, classes, and methods with unique scope and purpose. Rules for accessing properties, variables, and methods in other encapsulated scopes depend on visibility. This article explains how visibility is defined, how it works, and why visibility is important in OOP PHP. Keep in mind that this document references how these principles work in PHP 5.4 or later. PHP 5.3 does not have the main functions for properly executing OOP. It runs much slower than 5.4 and lacks other important grammatical functions of the language. I think the security support is insufficient, and no one can use or support it. The
In packaging and scope software design, we believe that packaging is the principle of binding code and data together to restrict access to other programming. The simplest example of encapsulation is a function. Consider the following PHP code:$ Post = get_post (1);
Function slug\u get\post\u five(){
$post = get_post (5);
Var_dump ($post);
}The
Var_dump ($post);
Slug\u get\u post\u five (5); In this example without object-oriented PHP, \
In this case, the second $post is encapsulated as a \
Instead, OOP provides the ability to control the access of variables in classes whose symmetry is an attribute. This is one of the main advantages of OOP. Class properties and methods have three levels of visibility. Before ensuring the visibility of classes to objects, I want to quickly distinguish between classes and objects. Class defines the rule set for the created object. Objects are created by instantiating classes that determine what objects can do. You can always see it in the WordPress where the wp_query class is located. In the global variable, \
Each wp_query object has the same method and property, but the value of the property or the result of the method may be different. For example, suppose you request the URL of the category term archive file, list the posts of the custom post type \
Use completely different objects, although s. The
Using one class to create multiple objects for similar purposes is a good software design. If you need to create a custom database query every time you need to import several word press posts from the database, it will be difficult. You can also extend this class by adding a second class (called a subclass), which is a subclass of the rules for adding and modifying parent classes. This is important to understand how visibility works and how it works. Subclasses can override properties or methods. Three visibility levels OOP PHP has three visibility levels (public, protected, and private), which are class properties and methods. Visibility uses visibility keyword declarations to declare the visibility level of a property or method. Three levels define whether a property or method can be accessed from outside the class and from within the class that extends the class. The
The first level of public is \
The second level of protection is \
View this code, which shows three real estate visibility rule classes. The second and third classes extend the first class, and the third class also violates the visibility rules within the class, so an error is generated. Quasi posture{
\/**The
*This protected property is accessible by subclasses.
*\/The
Protected Jedi;
\/**The
*Subclasses cannot access this private property.
*\/The
Personal $Sith;
Public function set_force_users ($jedi, $sith){
\/\/Both are legal. Because we are in the same class. Private is not a meaningful distinction from protected.
$this->jedi = $jedi;
$this->sith = $sith;
}The
}The
Jedi like Knights expand power{
\/**The
*This is a good example.
*\/The
Public function set_jedi ($jedi){
\/\/Jedi Knights are protected, so they can be accessed from this subclass, so they are completely legal.
$this->jedi = $jedi;
}The
}The
Perspective like extension force{
\/**The
*Examples of bad things, like Sith, you know, come on.
*\/The
Public function set_sith ($sith){
\/\/Not allowed, the private property of the parent cannot be set in a subclass.
$this->sith = $sith;
}The
}Let’s take a look at this process. The base class declares two properties. The first attribute \
Consider the following categories: Lightsaber like{
Public $name;
Protected $color;
Public function set_name ($name){
If (is_string ($name){
$i-> name = $name;
}The
}The
}In this case, you can set any property name. This will be a problem if you later write code that uses \

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *