Rumah > pembangunan bahagian belakang > tutorial php > PHP新手之编程快速入门_PHP教程

PHP新手之编程快速入门_PHP教程

WBOY
Lepaskan: 2016-07-20 10:57:44
asal
1164 orang telah melayarinya

面向对象编程(OOP)是我们编程的一项基本技能,PHP4对OOP提供了良好的支持。如何使用OOP的思想来进行PHP的高级编程,对于提高PHP编程能力和规划好Web开发构架都是非常有意义的。下面我们就通过实例来说明使用PHP的OOP进行编程的实际意义和应用方法。

我们通常在做一个有数据库后台的网站的时候,都会考虑到程序需要适用于不同的应用环境。和其他编程语言有所不同的是,在PHP中,操作数据库的是一系列的具体功能函数(如果你不使用ODBC接口的话)。这样做虽然效率很高,但是封装却不够。如果有一个统一的数据库接口,那么我们就可以不对程序做任何修改而适用于多种数据库,从而使程序的移植性和跨平台能力都大大提高。

在PHP中要完成OOP,需要进行对象封装,也就是编写类。我们可以通过生成一个新的SQL类实现对数据库的简单封装。

例如:

<ol class="dp-c">
<li class="alt"><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></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>
</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>
Salin selepas log masuk

我们以操作MySQL数据库为例。我们写一个数据库驱动类MySQL,在该类中,我们把有关MySQL数据库操作的函数都做进一步的封装。把包含该类,文件名为MySQL.php的文件放在PHP的系统 include_path下,就可以正常地使用了。注意编写数据库驱动文件时,文件名应和类名保持一致。

<ol class="dp-c">
<li class="alt"><span><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></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>
</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 class="vars">$rowno</span><span>;</span><span class="vars">$row</span><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>
Salin selepas log masuk

同样我们要封装其他的“数据库驱动”到我们的SQL类中,只需要建立相应的类,并以同名命名驱动文件,放到PHP的include目录就可以了。

完成封装以后,就可以在PHP中按照OOP的思想来实现对数据库的编程了。

<ol class="dp-c">
<li class="alt"><span><span></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>
</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>
Salin selepas log masuk

在实际应用中,我们还可以根据实际需求对各种对象类做进一步扩展。在PHP中,还提供了一系列复杂的OOP方法,例如继承,重载,引用,串行化等等。充分调动各种方法并灵活运用,就能够使你的网站更合理和结构化,开发和维护也更容易。

希望通过以上内容的介绍,能给你带来帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445751.htmlTechArticle面向对象编程 (OOP)是我们编程的一项基本技能, PHP 4对OOP提供了良好的支持。如何使用OOP的思想来进行PHP的高级编程,对于提高PHP编程能...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan