search
HomeBackend DevelopmentPHP TutorialBasic knowledge of PHP object-oriented programming_PHP tutorial

Basic knowledge of PHP object-oriented programming_PHP tutorial

Jul 15, 2016 pm 01:28 PM
phpunderforintroduceaboutbasic knowledgeExampleobjectusyesofprogrammingexplainillustratepassFor

What we are introducing to you today is about Below we will use examples to illustrate the practical significance and application methods of using PHP object-oriented 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.

The completion of PHP object-oriented programming requires object encapsulation, that is, writing classes. We can achieve a simple encapsulation of the database by generating a new SQL class. For example:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> ?  </span></span></li>
<li class=""><span>class SQL  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>var $Driver; //实际操作的数据库驱动子类  </span></li>
<li class="alt"><span>var $connection; //共用的数据库连接变量  </span></li>
<li class=""><span>function DriverRegister($d)  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>if($d!="")  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">include_path</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">ini_get</font></span><span>("include_path");  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">DriverFile</font></span><span> = $include_path."/".$d.".php";  </span>
</li>
<li class=""><span>//驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下  </span></li>
<li class="alt"><span>if( file_exists( $DriverFile)) //查找驱动是否存在  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>include($DriverFile);  </span></li>
<li class="">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">Driver</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> $d();  </span>
</li>
<li class="alt"><span>// 根据驱动名称生成相应的数据库驱动类  </span></li>
<li class=""><span>return true;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>return false; //注册驱动失败  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>function Connect($host,$user,$passwd,$database)//连接数据库的函数  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">host</font></span><span>=$host;  </span>
</li>
<li class="">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">user</font></span><span>=$user;  </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">passwd</font></span><span>=$pas  </span>
</li>
<li class=""><span>swd;  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">database</font></span><span>=$d  </span>
</li>
<li class=""><span>atabase;  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">connection</font></span><span> = $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Connect();  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>function Close()//关闭数据库函数  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>close($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>connection);  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>function Query($queryStr)//数据库字符串查询函数  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>query($queryStr,$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>connection);  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>function getRows($res)//查找行  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getRows($res);  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>function getRowsNum($res)//取得行号  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Driver-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> getRowsNum ($res);  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>? </span><span class="tag"><strong><font color="#006699">></font></strong></span><span>  </span>
</li>
</ol>

I hope that the knowledge about PHP object-oriented programming introduced above will be useful to everyone. helped.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446417.htmlTechArticleWhat we will introduce to you today is about the following. We will use examples to illustrate the practical significance and use of PHP object-oriented programming. Application method. We usually work on a database background...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)