Implement data protection using php oop features

Source: Internet
Author: User
In PHP4, var is usually used to declare variables, while in PHP5, the object-oriented programming (OOP) feature can be applied to customize data visibility-accessibility, the visibility is very similar to the variable scope, but provides a better control mechanism.

In PHP 4, var is usually used to declare variables, while in PHP 5, object-oriented programming (OOP) features can be applied to customize data visibility-accessibility, visibility in this and variable scope is very similar, but provides a better control mechanism, there are three types of visibility modifier:

Public (default) -- variables can be visited or corrected globally.
Protected-variables can only be visited or corrected in the class itself and directly derived (using the extends statement) class.
Private -- variables can only be accessed or corrected within the class.

Similar to interface implementation, violating these rules in a program will lead to serious errors. what is similar to interfaces is that they exist purely for the convenience of programmers. However, this does not mean that you can ignore them and specify the visibility of a class member variable to protect the data in the object from external influences.

Assume that there is a MySqlDB class, where a $ link variable is declared as private, which means that this variable can only be accessed from the application of $ this variable in the object, this prevents unexpected coverage of other objects or functions outside the class. here, we will apply visibility features to help us create a query object.

You can regard a query as a separate entity, which can be executed and return results. Some database systems also have stored procedures. stored procedures are similar to functions. they store query statements and receive corresponding parameters during calls, however, MySQL did not provide similar functions before MySQL version 5.1, and some other types of database management systems did not.

In this article, the two features are combined into the query object in the example. The example imitates a basic stored procedure and retains the results pointer internally. Currently, the focus is to execute the query from the object, where you can call the query () function of the MySqlDB object.

The following public functions can be defined in the query object:

_ Construct () -- The structure function receives a parameter that contains the instance reference that implements the DB interface object.

Prepare () -- function prepare () initializes the stored procedure of the query. It may contain one or more limited placeholders, which will be passed as parameters to the execute () function. A placeholder is defined as a colon related to the number of parameters followed by an integer and a letter related to the parameter type.

A simple query containing placeholders looks like the following:

SELECT col1, col2 FROM table_name WHERE col1 =: 1I
Execute () -- The execute () function will perform the query. If it is initialized to a stored procedure too early by the prepare () function, any passed parameters will be used as the fulfillment parameters of the stored procedure. otherwise, the first parameter is only used as the query text. The execute () function returns the result after performing the query.

Compile () -- The compile () function is similar to the execute () function. In fact, the query is not executed. Instead, all placeholders in the query string are exchanged to receive parameters of the stored procedure, and return the compiled query version.

  Protected member

As mentioned above, the concept of visibility can be used to conceal the internal work of objects and protect the data integrity required for internal work. As mentioned above, the results pointer returned by the query will be retained as the protected attribute. in this application, the member protection is due to the fact that the query object derived from the query object may overload some core functions.

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.