简体   繁体   English

如何组织PHP中的类并从类中访问数据库

[英]How do I organize classes in PHP and access database from within class

I have been programming procedural PHP for about 8 years, and am finally starting to learn OOPHP. 我从事程序PHP已有8年的编程经验,最终开始学习OOPHP。 I've gone through many many tutorials and I understand the syntax and feel quite comfortable using it. 我已经阅读了许多教程,并且我理解语法并且对使用它感到很自在。 What I've failed to find, though, is a practical example of how to setup the class file structure, and how to access my database from within classes to perform queries in them. 但是,我找不到的是一个实际示例,说明如何设置类文件结构以及如何从类内部访问数据库以在其中执行查询。

my initial thought was to create a separate folder called /class and make a file for every class. 我最初的想法是创建一个名为/ class的单​​独文件夹,并为每个类创建一个文件。 if a class extended another class, i would include() the parent file in the child file. 如果一个类扩展了另一个类,我将在子文件中include()父文件。 in my main scripts i would call classes as i need them. 在我的主要脚本中,我会根据需要调用类。 for example include(/class/member.php) when i needed them member class. 例如include(/class/member.php)当我需要它们成员类时。

also, i wrote a database class that extends mysqli (i know many of you will suggest i use PDO instead, and if you have some reasons why feel free to offer them, although i will say i don't expect to ever not use MySQL). 另外,我写了一个扩展mysqli的数据库类(我知道你们中的许多人会建议我改用PDO ,并且如果您出于某些原因愿意随时提供它们,尽管我会说我不希望永远不要使用MySQL。 )。 The part that really threw me was how to open the database connection in my classes. 真正让我兴奋的部分是如何在我的课程中打开数据库连接。 for example, my thought was in the member class I would pass a member_id , and then i would execute a query in the class to set all of its properties to that of the member. 例如,我的想法是在成员类中传递一个member_id ,然后在该类中执行查询以将其所有属性设置为该成员的属性。 Initially i included the database class in my main script and made an instance of it, and then declared the handle as global in my member class (I've read how globals are evil, and i don't really like that solution anyway). 最初,我在主脚本中包含数据库类并为其创建了一个实例,然后在我的成员类中将句柄声明为全局句柄(我已经阅读了全局变量是多么邪恶,而且我还是不太喜欢这种解决方案)。 what is a better way to do this? 有什么更好的方法可以做到这一点?

thanks for any help! 谢谢你的帮助!

In my opinion, the best way to learn OOP is to view already existing codebases which follow OOP. 我认为,学习OOP的最好方法是查看遵循OOP的现有代码库。 I would suggest checking out the Zend Framework API Docs . 我建议检查一下Zend Framework API Docs If it looks promising to you, download the framework and start practicing with it. 如果您觉得它很有希望,请下载该框架并开始使用它。

The cool thing about Zend Framework is it can be used as a top down MVC, or a stand-alone library, and is heavily used in the PHP community. Zend Framework的酷之处在于它可以用作自顶向下的MVC或独立的库,并且在PHP社区中大量使用。

Some things to concentrate on: 需要注意的一些事情:

  • Class autoloading 类自动加载
  • How class names line up with their respective directory structs 类名如何与其各自的目录结构对齐
  • How classes are extended 如何扩展课程
  • Interfaces 介面
  • Abstract classes 抽象类

First, you're definitely on the right track with the separate file for each class. 首先,每个课程都有单独的文件,这绝对是对的。 This is pretty much considered standard practice at this point. 在这一点上,这几乎被认为是标准做法。 I would look into class autoloading and spl_autoload_register . 我将研究类autoloadingspl_autoload_register I highly recommend setting up an autoloader first thing, trust me when I say you really don't want to have to do countless interface_exists and class_exists calls before you include your class files. 我强烈建议首先设置一个自动加载器,当我说您真的不想在包含类文件之前不必进行无数次interface_existsclass_exists调用时,请相信我。

Second, about your database calls. 其次,关于您的数据库调用。 I answered a similar question here but the basic gist is that you create a single database instance and inject that into the classes needing database access. 在这里回答了类似的问题,但基本要点是创建一个数据库实例并将其注入需要数据库访问的类中。 As far as mysqli , if that is what you like using go for it. 至于mysqli ,如果那是您喜欢使用的功能。 Its awesome that you aren't using the older, deprecated mysql_* . 非常棒,您没有使用旧的,已弃用的mysql_*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM