PHP Quick Start _php tips for object-oriented programming

Source: Internet
Author: User
Tags foreach php programming
"Abstract" object-oriented programming (OOP) is one of our basic skills in programming, and PHP4 provides good support for OOP. How to use OOP idea to do advanced PHP programming is very meaningful for improving PHP programming ability and planning web development architecture.

Object-oriented Programming (OOP) is a basic skill in our programming, and PHP4 provides good support for OOP. How to use OOP idea to do advanced PHP programming is very meaningful for improving PHP programming ability and planning web development architecture. Here we illustrate the practical meaning and application of programming using PHP's OOP by examples.

We usually do a Web site with a database background, we will take into account the application needs to apply to different environments. Unlike other programming languages, in PHP, the operation of a database is a series of specific functional functions (if you do not use an ODBC interface). This is highly efficient, but not enough to encapsulate. If there is a unified database interface, then we can not make any changes to the program to apply to a variety of databases, so that the porting of the program and cross-platform capabilities are greatly improved.

To complete oop in PHP, you need to encapsulate the object, which is to write the class. We can implement a simple encapsulation of the database by generating a new SQL class. For example:
Php:
Copy Code code as follows:
?
Class SQL
{
var $driver; Database-driven subclass of the actual operation
var $connection; Shared Database connection variables

function Driverregister ($d)
{
if ($d!= "")
{
$include_path = Ini_get ("include_path");
$driverfile = $include_path. " /". $d.". PHP ";
The drive store path must be under Include_path set in the php.ini file
if (file_exists ($driverfile))//Find driver exists
{
Include ($driverfile);
$this->driver = new $d ();
Generate a corresponding database-driven class based on the driver name
return true;
}
}
return false; Registration driver failure
}

Function connect ($host,$user,$passwd,$database)//Connection database functions
{
$this->driver->host=$host;
$this->driver->user=$user;
$this->driver->passwd=$passwd;
$this->driver->database=$database;
$this->connection = $this->driver->connect ();
}

function close ()//Closing database functions
{
$this->driver->close ($this->connection);
}

function query ($QUERYSTR)//database string query functions
{
Return $this->driver->query ($querystr,$this->connection);
}

function GetRows ($res)/lookup row
{
Return $this->driver->getrows ($res);
}

function Getrowsnum ($res)//Get line number
{
Return $this->driver-> Getrowsnum ($res);
}
}
?>




Let's take the MySQL database for example. We write a database-driven class MySQL, in which we do a further encapsulation of functions related to MySQL database operations. The file containing this class, file name mysql.php is placed in the PHP system include_path, you can use the normal. Note When writing a database-driven file, the file name should be consistent with the class name.
Php:
Copy Code code as follows:
?
Class MySQL
{
var $host;
var $user;
var $passwd;
var $database;
function MySQL ()//use constructor to implement variable initialization
{
$host = "";
$user = "";
$passwd = "";
$database = "";
}

function Connect ()
{
$conn = mysql_connect ($this->host, $this->user,$this->passwd) or
Die ("Could not con nect to $ this->host");
mysql_select_db ($this->database,$conn) or
Die ("Could not SWI tch 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 que ry 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;
}
}
?>


Also, we want to encapsulate other "database drivers" into our SQL classes, just create the appropriate classes and name the driver files with the same name, and put them in the PHP include directory.

After the encapsulation is complete, you can implement the programming of the database in PHP according to OOP idea.

Php:
Copy Code code as follows:
?
Include ("sql.php");
$sql = new < font color= "#0000bb" >SQL; To generate a new SQL object
if ($sql-> driverregister ("MySQL" & Lt;font color= "#007700" >))//Register database driver
{
$sql->connect ("localhost", "root" &l t;font color= "#007700" "" "," Test "&l T;font" #007700 ">);
$res=$sql->query ("Select & Lt;font color=" #007700 ">* from Test"); Return query recordset
$rowsnum = $sql->getrowsnum ($res);
if ($rowsnum > 0)
{
$rows = $sql->getrows ($res);
foreach ($rows as $row)//loop FETCH Recordset Contents
{
foreach ($row as $field) {
Print $field;}
}
}
$sql->close ();
}
?>


In practical application, we can further expand the various object classes according to the actual demand. In PHP, it also provides a series of complex OOP methods, such as inheritance, overloading, referencing, serialization, and so on. The full mobilization of various methods and flexible use, can make your site more reasonable and structured, development and maintenance is also easier.

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.