与其他类共享参数类

时间:2014-07-14 14:55:13

标签: php

我想分享从db中的表中获取的参数。 为了获取这些参数,我创建了一个为此而创建的类。 这是我的场景,分享各个类之间的数据库中包含的参数。这样做的正确方法是什么?

class Database 
{
    $private mys; 

    public function __construct()
    {
        $this->mys = new mysqli(....); 
    }
}

class params
{ 
    $private db;
    $public var1;
    $public var2;

    public function __construct()
    {
          $this->db = new Database();
    } 

    public function getParams()
    {
        $result = $this->db->mys->query ("SELECT * FROM params");

           while($row = $result->fetch_assoc()) 
           {
                $this->var1 = $row['var1'];
                $this->var2 = $row['var2'];
           }    
    }

}

class foo
{
    private $db;
    private $ps;

    public function __construct()
    {
        $this->db = new Database;
        $this->ps = new Params;

    }

    public function viewParams()
    {
          echo $this->ps->var1;
          echo $this->ps->var2;
    }

 }

2 个答案:

答案 0 :(得分:0)

要从其他类访问私有字段,请声明公共'getter'方法,例如:

public getVal1() {
 return $this->val1;
}

答案 1 :(得分:0)

我谦虚地认为,有多种方法可以与其他类共享参数类。它们取决于您的系统目前是如何以及它将如何发展。

根据我的学习,将依赖项传递给类的正确方法是将它们注入构造函数,而不是:

class foo
{
    ...
    public function __construct()
    {
        $this->db = new Database;

我更愿意首先实现$db

$db = new Database();

然后

class foo
{
    ...
    public function __construct(Database $db)
    {
        $this->db = $db;

通过这种方式,你的课程松散耦合,有一天你可以轻松地负担单元测试,使用接口而不是具体的实现等等......请看看http://en.wikipedia.org/wiki/Dependency_injection

作为个人建议我会尝试将params类设计为实体,映射为Db表对象:

//entityParams.php
class EntityParams
{
    private $id; //column name
    private $columnA;
    private $columnB; 

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setColumnA($columnA)
    {
        $this->columnA = $columnA;
    }
    ...

}

然后使用Database类作为简单的连接器类(仅限于promotes single-responsibility and separation of concerns):

//Database.php
class Database
{
    private $dbh = NULL;

    public function connect()
    {
        $this->dbh = new Mysqli(...);
    }

    public function getConnection()
    {
        if(is_null($this->dbh))
        {
            $this->connect();
        }

        return $this->dbh;
    }
}

并使用DatabaseManager来让它完成所有脏工作。例如:

//DatabaseManager.php
class DatabaseManager
{
    private $db;
    private $entities = array();
    private $currentEntity;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function fromEntity($entityName)
    {
        $entityClass = "Entity".ucfirst($entityName);

        if(!isset($this->entities[$entityClass]))
        {
            $this->entities[$entityClass] = $entityName;
        }

        $this->currentEntity = $entityClass;

        return $this;
    }

    public function getAll()
    {
        $results = $this->db->getConnection()->query("SELECT * FROM {$this->entities[$this->currentEntity]}");

        $entities = array();

        foreach ($results->fetch_all(MYSQLI_ASSOC) as $key => $item) 
        {
            $e = new $this->currentEntity;
            $e->setId($item['id']);
            $e->setColumnA($item['columnA']);
            $e->setColumnB($item['columnB']); 

            $entities[] = $e;
        }

       return $entities;
   }

最后你的Foo类(以及其他所有人)只与DatabaseManager(它可以演变为RespositoryManager)有一个简单的依赖关系:

//Foo.php
class Foo
{
    public $dbm;

    public function __construct(DatabaseManager $dbm)
    {
        $this->dbm = $dbm;
    }

    public function viewParams()
    {
        return $this->dbm->fromEntity("params")->getAll();
    }

    public function viewParam($id)
    {
        return $this->dbm->fromEntity("params")->find(2);
    }
}    

//client.php
/* Here you can instantiate the classes and pass they through constructor or investigate on how to create and use a Dependency Injection Container */
$db = new Database();
$dbm = new DatabaseManager($db);

$foo = new Foo($dbm);

var_dump($foo->viewParams());
var_dump($foo->viewParam(1));

我刚写了一些简单的基本想法,值得围绕它进行改进并改进它。