Using PHP's OOP feature to realize data protection

Source: Internet
Author: User
Tags prepare visibility

In PHP 4, declaring a variable usually uses VAR, in PHP 5, you can use object-oriented programming (OOP) features to customize the visibility of data-accessibility, which is similar to the scope of the variable, but provides a better control mechanism with the following three types of visibility modifiers:

Public (default)--variables can be accessed or modified globally. The
protected--variable can only be accessed or modified within the class itself and directly from the class itself (using extends statements). The
private--variable can only be accessed or modified within the class.

, similar to interface implementations, can cause serious errors in programs that violate them, and, like interfaces, exist purely for the convenience of programmers. However, this does not mean that you can ignore them, specify the visibility of a class member variable, and protect the data within the object from external influences.

assumes that there is a mysqldb class in which a $link variable is declared private, meaning that the variable can only be accessed from within the object using the $this variable, which prevents accidental overwriting of other objects or functions outside the class, We'll use the visibility feature to help us create a query object.

You can think of query as a separate entity that can execute and return results. Some database systems also have stored procedures that are similar to functions, store query statements, and accept the corresponding arguments when invoked, but MySQL does not provide similar functionality until version 5.1, and some other types of database management systems do not.

In this article, the two attributes are combined into the sample query object, and the example simulates a basic stored procedure and saves the result pointer internally. Currently, the focus is on executing query from the object, where you can invoke the query () function of the MySQLdb object.

can define the following public function in the query object:

__construct ()--the constructor accepts a parameter that contains a reference to an instance of the DB interface object. The

Prepare ()--function prepare () initializes the stored procedure for query. It may contain one or more limited placeholders that will be passed as arguments to the Execute () function. A placeholder is defined as a colon that is associated with the number of parameters followed by an integer and a letter related to the parameter type. A simple query that contains placeholders for

looks like this:
 

SELECT col1,col2 from table_name WHERE col1=:1i



Execute ()--the function execute () executes query. If it is prematurely initialized to a stored procedure by the prepare () function, any parameters passed in are used as the execution parameters of the stored procedure, otherwise the first argument will only be used as the query text. The function execute () returns the result of executing the query.







Compile ()--function compile () is similar to the function execute (), in fact, query does not execute, but instead replaces all placeholders in the query string, accepts the parameters of the stored procedure, and returns the compiled version of query.

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.