本文介绍了在php中正确地使用其他类中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 很久以前应该问过某人。 在其他课程中使用其他课程的最佳方法是什么? 例如,假设我有一个应用程序类: 类应用程序 { public function displayVar(){ echo'hello world'; } } 和数据库类 类数据库 { //连接到db on construct public function query(){ / / queries db } } 现在,我想添加一个函数我的应用程序类使用db类中的函数 类应用程序 { public function displayVar (){ echo'hello world'; } public function getVar(){ global $ db; $ sql = foo; $ db-> query($ sql); } } 所以我有 $ db = new Database(); $ app = new Application(); $ app-> getVar('var'); 有更好的方法吗? 解决方案有一对夫妇的做法的方式。全局变量当然是一种方式,也最看不起。您可以创建 Singleton 和所有需要数据库访问的其他类调用此单例。 final类数据库{ private static $ connection; public static function getInstance(){ if(self :: $ connection == NULL){ self :: $ connection = //你的数据库连接} return self :: $ connection; } } 并使用此数据库连接对象在任何类需要它。 类应用程序{ public function displayVar(){ echo'hello world'; } public function getVar(){ $ db = Database :: getInstance(); $ sql = foo; $ db-> query($ sql); } } 这是一个很好的开始和伟大的一步超越使用全局变量,但您可以通过依赖注入做得更好。依赖注入是一个简单的概念,如果一个类有任何外部依赖,例如你的示例中的数据库连接,你显式地传递给needy类在其构造函数或方法。所以新的代码看起来像Jonathan的解决方案。使用依赖注入的一个主要优点是在单元测试中,你可以很容易地用一个mock对象替换这个实际的数据库对象,并将它传递给任何需要它的人。 class Application { private $ db; public function __construct(Database $ db){ $ this-> db = $ db; } public function displayVar(){ echo'hello world'; } public function getVar(){ $ sql = foo; $ this-> db-> query($ sql); } } 对于较小的项目,您可以自己轻松地做。对于大型项目,有各种 DI框架可用于PHP Should have asked someone this a long time ago.What is the best way to use other classes within another class?For instance, lets say I have an application class:class Application{ public function displayVar() { echo 'hello world'; }}and a database classclass Database{ // connects to db on construct public function query() { // queries db }}now, i want to add a function to my application class that uses a function from the db classclass Application{ public function displayVar() { echo 'hello world'; } public function getVar() { global $db; $sql = foo; $db->query($sql); }}so then I have$db = new Database();$app = new Application();$app->getVar('var');Is there a better way of doing this? Really what I am looking for is the standard way of doing it, not another way of rigging it. 解决方案 There are a couple of ways of doing that. Global variables is certainly one way and the most looked down upon too. You can create a Singleton and all other classes that need database access would call upon this singleton.final class Database { private static $connection; public static function getInstance() { if(self::$connection == NULL) { self::$connection = // init your database connection } return self::$connection; }}And use this database connection object in whatever class needs it.class Application { public function displayVar() { echo 'hello world'; } public function getVar() { $db = Database::getInstance(); $sql = foo; $db->query($sql); }}This is all well for a start and a great step beyond using global variables, but you can do better with Dependency Injection. Dependency Injection is a simple concept that if a class has any external dependencies, such as the database connection in your example, you explicitly pass those to the needy class in its constructor or a method. So the new code would look something like Jonathan's solution. A major advantage of using dependency injection is in unit testing, where you can easily replace this actual database object with a mock object and pass it to whoever needs it.class Application { private $db; public function __construct(Database $db) { $this->db = $db; } public function displayVar() { echo 'hello world'; } public function getVar() { $sql = foo; $this->db->query($sql); }}For smaller projects, you can easily do it yourself. For large projects, there are various DI frameworks available for PHP 这篇关于在php中正确地使用其他类中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-29 05:57
查看更多