PHP Object-Oriented Programming Quick Start
Document Sample


PHP Object-Oriented Programming Quick Start Article From:http://www.tutorialsto.com/php/base/php-object-oriented-programming-quick-start.html Object-oriented programming (OOP) is one of our basic skills program, PHP4 on oop provides a good support. The idea of how to use OOP for PHP, senior programming, php programming for improving the capacity and planned framework for Web development is very significant. We have adopted the following examples to illustrate the use of PHP's OOP programming and application of practical significance. We usually do a database in the background when the site will need to take into account the procedures applicable to different application environments. And different from other programming languages, in PHP, the operation of the database is a series of specific function (if you do not, then use the ODBC interface). Although very efficient to do so, but the package is not enough. If there is a unified database interface, then we can not make any modification procedures applied to a variety of databases, so that the transplant procedure and cross-platform capabilities are greatly enhanced. PHP should be completed in the OOP, the need for the target package, which is the preparation of category. We can generate a new type of SQL database to achieve a simple package. For example: <? class SQL ( var $ Driver; / / the actual operation of the database-driven sub-class var $ connection; / / share the database connection variables function DriverRegister ($ d) ( if ($ d !="") ( $ include_path = ini_get ( "include_path"); $ DriverFile = $ include_path ."/".$ d. ". Php"; / / Drive storage PHP.ini file path must be set under the INCLUDE_PATH if (file_exists ($ DriverFile)) / / find the existence of driver ( include ($ DriverFile); $ this-> Driver = new $ d (); / / Under the driver name to generate the corresponding database-driven type return true; ) ) return false; / / registration drive failure ) function Connect ($ host, $ user, $ passwd, $ database) / / connect to database function ( $ this-> Driver-> host = $ host; $ this-> Driver-> user = $ user; $ this-> Driver-> passwd = $ pas swd; $ this-> Driver-> database = $ d atabase; $ this-> connection = $ this-> Driver-> Connect (); ) function Close () / / close the database function ( $ this-> Driver-> close ($ this-> connection); ) function Query ($ queryStr) / / database query string function ( return $ this-> Driver-> query ($ queryStr, $ this-> connection); ) function getRows ($ res) / / find line ( return $ this-> Driver-> getRows ($ res); ) function getRowsNum ($ res) / / get line number ( return $ this-> Driver-> getRowsNum ($ res); ) ) ?> We mysql database to operate as an example. We write a database-driven type of MySQL, in this kind, we have the MySQL database to operate all the functions of the package further. To contain such, the document file name MySQL.php on PHP's include_path under System, you can normally use. Attention to the preparation of database-driven document, the file name and class name should be consistent. <? Class MySQL ( var $ host; var $ user; var $ passwd; var $ database; function MySQL () / / use constructor to initialize variables to achieve ( $ host = ""; $ user = ""; $ passwd = ""; $ database = ""; ) function Connect () ( $ conn = MySQL_connect ($ this-> host, $ this-> user, $ this-> passwd) or die ( "Could not connect to $ this-> host"); MySQL_select_db ($ this-> database, $ conn) or die ( "Could not switch to database $ this-> database;"); return $ conn; ) function Close ($ conn) ( MySQL_close ($ conn); ) function Query ($ queryStr, $ conn) ( $ res = MySQL_query ($ queryStr, $ conn) or die ( "Could not query database"); return $ res; ) function getRows ($ res) ( $ rowno = 0; $ rowno = MySQL_num_rows ($ res); if ($ rowno> 0) ( for ($ row = 0; $ row <$ rowno; $ row + +) ( $ rows [$ row] = MySQL_fetch_row ($ res); ) return $ rows; ) ) function getRowsNum ($ res) ( $ rowno = 0; $ rowno = mysql_num_rows ($ res); return $ rowno; ) ) ?> Similarly, we have to package other "database-driven" to the SQL type we only need to establish the appropriate category, and named the same name, driver files, the include directory of PHP on it. After the completion of packaging, we can according to OOP in PHP to realize the idea of the programming of the database. <? Include ( "SQL.php"); $ sql = new SQL; / / generate new object Sql if ($ sql-> DriverRegister ( "MySQL")) / / registration database-driven ( $ sql-> Connect ( "localhost", "root ",""," test"); $ res = $ sql-> query ( "select * from test"); / / return query recordset $ rowsnum = $ sql-> getRowsNum ($ res); if ($ rowsnum> 0) ( $ rows = $ sql-> getRows ($ res); foreach ($ rows as $ row) / / cycle sets out the contents of records ( foreach ($ row as $ field) ( print $ field;) ) ) $ sql-> Close (); ) ?> In practice, we can according to the actual needs of the various object classes for further expansion. In PHP also provides a series of complex OOP methods, such as inheritance, overloading, references, and so on serialization. Fully mobilized and flexibility in the use of various methods, we can make your site a more rational and structured, the development and maintenance easier. About the Author Source: http://www.tutorialsto.com
Related docs
Get documents about "