Home >Backend Development >PHP Tutorial >A quick introduction to programming for PHP newbies_PHP tutorial
Object-oriented programming (OOP) is a basic skill for our programming, and PHP4 provides good support for OOP. How to use OOP ideas to perform advanced programming of PHP is very meaningful for improving PHP programming capabilities and planning a good Web development architecture. Below we will use examples to illustrate the practical significance and application methods of using PHP's OOP for programming.
When we usually build a website with a database backend, we will consider that the program needs to be suitable for different application environments. What is different from other programming languages is that in PHP, a series of specific functions are used to operate the database (if you do not use the ODBC interface). Although this is very efficient, the encapsulation is not enough. If there is a unified database interface, then we can apply it to a variety of databases without making any modifications to the program, thus greatly improving the portability and cross-platform capabilities of the program.
To complete OOP in PHP, you need to encapsulate objects, that is, write classes. We can achieve a simple encapsulation of the database by generating a new SQL class.
For example:
<ol class="dp-c"> <li class="alt"><span><span>< ? </span><span class="keyword">class</span><span> SQL { </span><span class="keyword">var</span><span> </span><span class="vars">$Driver</span><span>; </span></span></li><li><span class="comment">//实际操作的数据库驱动子类 </span><span> </span></li><li class="alt"><span class="keyword">var</span><span> </span><span class="vars">$connection</span><span>; </span></li><li><span class="comment">//共用的数据库连接变量 </span><span> </span></li><li class="alt"><span class="keyword">function</span><span> DriverRegister(</span><span class="vars">$d</span><span>) { </span></li><li><span class="keyword">if</span><span>(</span><span class="vars">$d</span><span>!=</span><span class="string">""</span><span>) { </span><span class="vars">$include_path</span><span> = </span><span class="func">ini_get</span><span>(</span><span class="string">"include_path"</span><span>); </span></li><li class="alt"><span class="vars">$DriverFile</span><span> = </span><span class="vars">$include_path</span><span>.</span><span class="string">"/"</span><span>.</span><span class="vars">$d</span><span>.</span><span class="string">".php"</span><span>; </span></li><li><span class="comment">//驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下 </span><span> </span></li><li class="alt"><span class="keyword">if</span><span>( </span><span class="func">file_exists</span><span>( </span><span class="vars">$DriverFile</span><span>)) </span><span class="comment">//查找驱动是否存在 </span><span> </span></li><li><span>{ </span></li><li class="alt"><span class="keyword">include</span><span>(</span><span class="vars">$DriverFile</span><span>); </span><span class="vars">$this</span><span>->Driver = </span><span class="keyword">new</span><span> </span><span class="vars">$d</span><span>(); </span></span></li> <li> <span class="comment">// 根据驱动名称生成相应的数据库驱动类 </span><span> </span> </li> <li class="alt"> <span class="keyword">return</span><span> true; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="keyword">return</span><span> false; </span> </li> <li class="alt"> <span class="comment">//注册驱动失败 </span><span> </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">function</span><span> Connect(</span><span class="vars">$host</span><span>,</span><span class="vars">$user</span><span>,</span><span class="vars">$passwd</span><span>,</span><span class="vars">$database</span><span>) </span> </li> <li> <span class="comment">//连接数据库的函数 </span><span> </span> </li> <li class="alt"> <span>{ </span><span class="vars">$this</span><span>->Driver->host=</span><span class="vars">$host</span><span>; </span> </li> <li> <span class="vars">$this</span><span>->Driver->user=</span><span class="vars">$user</span><span>; </span> </li> <li class="alt"> <span class="vars">$this</span><span>->Driver->passwd=</span><span class="vars">$pas</span><span> swd; </span> </li> <li> <span class="vars">$this</span><span>->Driver->database=</span><span class="vars">$d</span><span> atabase; </span> </li> <li class="alt"> <span class="vars">$this</span><span>->connection = </span><span class="vars">$this</span><span>->Driver->Connect(); </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">function</span><span> Close() </span> </li> <li> <span class="comment">//关闭数据库函数 </span><span> </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="vars">$this</span><span>->Driver->close(</span><span class="vars">$this</span><span>->connection); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> Query(</span><span class="vars">$queryStr</span><span>) </span> </li> <li class="alt"> <span class="comment">//数据库字符串查询函数 { </span><span> </span> </li> <li> <span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->Driver->query(</span><span class="vars">$queryStr</span><span>,</span><span class="vars">$this</span><span>->connection); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> getRows(</span><span class="vars">$res</span><span>) </span> </li> <li class="alt"> <span class="comment">//查找行 </span><span> </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->Driver->getRows(</span><span class="vars">$res</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">function</span><span> getRowsNum(</span><span class="vars">$res</span><span>) </span> </li> <li> <span class="comment">//取得行号 </span><span> </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->Driver-> getRowsNum (</span><span class="vars">$res</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>? > </span></li> </ol>
Let’s take operating the MySQL database as an example. We write a database driver class MySQL. In this class, we further encapsulate the functions related to MySQL database operations. Put the file containing this class and the file name is MySQL.php under the PHP system include_path, and it can be used normally. Note that when writing the database driver file, the file name should be consistent with the class name.
<ol class="dp-c"> <li class="alt"><span><span>< ? Class MySQL { </span><span class="keyword">var</span><span> </span><span class="vars">$host</span><span>; </span><span class="keyword">var</span><span> </span><span class="vars">$user</span><span>; </span><span class="keyword">var</span><span> </span><span class="vars">$passwd</span><span>; </span><span class="keyword">var</span><span> </span><span class="vars">$database</span><span>; </span></span></li><li><span class="keyword">function</span><span> MySQL() </span></li><li class="alt"><span class="comment">//利用构造函数实现变量初始化 </span><span> </span></li><li><span>{ </span><span class="vars">$host</span><span> = </span><span class="string">""</span><span>; </span></li><li class="alt"><span class="vars">$user</span><span> = </span><span class="string">""</span><span>; </span></li><li><span class="vars">$passwd</span><span> = </span><span class="string">""</span><span>; </span></li><li class="alt"><span class="vars">$database</span><span> = </span><span class="string">""</span><span>; </span></li><li><span>} </span></li><li class="alt"><span class="keyword">function</span><span> Connect() </span></li><li><span>{ </span><span class="vars">$conn</span><span> = MySQL_connect(</span><span class="vars">$this</span><span>->host, </span><span class="vars">$this</span><span>->user,</span><span class="vars">$this</span><span>->passwd) </span></span></li> <li> <span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">"Could not connect to $this->host"</span><span>); </span> </li> <li class="alt"> <span>MySQL_select_db(</span><span class="vars">$this</span><span>->database,</span><span class="vars">$conn</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">"Could not switch to database $this->database;"</span><span>); </span> </li> <li> <span class="keyword">return</span><span> </span><span class="vars">$conn</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> Close(</span><span class="vars">$conn</span><span>) { </span> </li> <li class="alt"> <span>MySQL_close(</span><span class="vars">$conn</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"> <span class="keyword">function</span><span> Query(</span><span class="vars">$queryStr</span><span>, </span><span class="vars">$conn</span><span>) </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="vars">$res</span><span> =MySQL_query(</span><span class="vars">$queryStr</span><span>, </span><span class="vars">$conn</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">"Could not query database"</span><span>); </span> </li> <li> <span class="keyword">return</span><span> </span><span class="vars">$res</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> getRows(</span><span class="vars">$res</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="vars">$rowno</span><span> = 0; </span> </li> <li class="alt"> <span class="vars">$rowno</span><span> = MySQL_num_rows(</span><span class="vars">$res</span><span>); </span> </li> <li> <span class="keyword">if</span><span>(</span><span class="vars">$rowno</span><span>>0) </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="keyword">for</span><span>(</span><span class="vars">$row</span><span>=0;</span><span class="vars">$row</span><span><</span><span class="vars">$rowno</span><span>;</span><span class="vars">$row</span><span>++) </span></li><li class="alt"><span>{ </span></li><li><span class="vars">$rows</span><span>[</span><span class="vars">$row</span><span>]=MySQL_fetch_row(</span><span class="vars">$res</span><span>); </span></li><li class="alt"><span>} </span></li><li><span class="keyword">return</span><span> </span><span class="vars">$rows</span><span>; </span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span class="keyword">function</span><span> getRowsNum(</span><span class="vars">$res</span><span>) </span></li><li><span>{ </span></li><li class="alt"><span class="vars">$rowno</span><span> = 0; </span></li><li><span class="vars">$rowno</span><span> = mysql_num_rows(</span><span class="vars">$res</span><span>); </span><span class="keyword">return</span><span> </span><span class="vars">$rowno</span><span>; </span></li><li class="alt"><span>} } ? > </span> </li> </ol>
Similarly, we need to encapsulate other "database drivers" into our SQL class. We only need to create the corresponding class, name the driver file with the same name, and put it in PHP The include directory is fine.
After completing the encapsulation, you can program the database in PHP according to OOP ideas.
<ol class="dp-c"> <li class="alt"><span><span>< ? Include(“SQL.php”); </span></span></li><li><span class="vars">$sql</span><span> = </span><span class="keyword">new</span><span> SQL; </span></li><li class="alt"><span class="comment">//生成新的Sql对象 </span><span> </span></li><li><span class="keyword">if</span><span>(</span><span class="vars">$sql</span><span>-> DriverRegister(“MySQL”)) </span></span></li> <li class="alt"> <span class="comment">//注册数据库驱动 </span><span> </span> </li> <li><span>{ </span></li> <li class="alt"> <span class="vars">$sql</span><span>->Connect(“localhost”,”root”,””,”test”); </span> </li> <li> <span class="vars">$res</span><span>=</span><span class="vars">$sql</span><span>->query(“select * from test”); </span> </li> <li class="alt"> <span class="comment">//返回查询记录集 </span><span> </span> </li> <li> <span class="vars">$rowsnum</span><span> = </span><span class="vars">$sql</span><span>->getRowsNum(</span><span class="vars">$res</span><span>); </span> </li> <li class="alt"> <span class="keyword">if</span><span>(</span><span class="vars">$rowsnum</span><span> > 0) { </span> </li> <li> <span class="vars">$rows</span><span> = </span><span class="vars">$sql</span><span>->getRows(</span><span class="vars">$res</span><span>); </span> </li> <li class="alt"> <span class="keyword">foreach</span><span>(</span><span class="vars">$rows</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$row</span><span>) </span> </li> <li> <span class="comment">//循环取出记录集内容 { </span><span> </span> </li> <li class="alt"> <span class="keyword">foreach</span><span>(</span><span class="vars">$row</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$field</span><span>){ print </span><span class="vars">$field</span><span>;} } } </span><span class="vars">$sql</span><span>->Close(); </span> </li> <li><span>} ? > </span></li> </ol>
In practical applications, we can further expand various object classes according to actual needs. In PHP, a series of complex OOP methods are also provided, such as inheritance, overloading, reference, serialization, etc. By fully mobilizing various methods and using them flexibly, you can make your website more reasonable and structured, and make it easier to develop and maintain.
I hope that the introduction of the above content can be helpful to you.