问题描述
很久以前应该问过某人。
在其他课程中使用其他课程的最佳方法是什么?
例如,假设我有一个应用程序类:
类应用程序
{
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');
有更好的方法吗?
有一对夫妇的做法的方式。全局变量当然是一种方式,也最看不起。您可以创建以及需要数据库访问权限的所有其他类将调用这个单例。
final类数据库{
private static $ connection;
public static function getInstance(){
if(self :: $ connection == NULL){
self :: $ connection = // init你的数据库连接
}
return self :: $ connection;
}
}
并使用此数据库连接对象在任何类需要它。
class Application {
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);
}
}
对于较小的项目,对于大型项目,有各种
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 class
class 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 class
class 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中正确地使用其他类中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!