PHP为类设置值

时间:2017-07-16 14:12:43

标签: php function class object

我有两个PHP类(DBOperations.php和functions.php)。现在我想在functions.php类中设置DBOperations.php类中的值。

class DBOperations{

    private $host = 'localhost';
    private $user = 'u';
    public $database;
    private $pass = 'p';
    private $conn;

public function __construct() {



    $this -> conn = new PDO("mysql:host=".$this -> host.";dbname=".$this -> database, $this -> user, $this -> pass);

    }
 public function doMagic() {
 //running mysql queries here
 }

和functions.php:

require_once 'DBOperations.php';

class Functions{

private $db;

public function __construct() {

       $this -> db = new DBOperations(); 
}
public function doThings($variable) {

  $db = $this -> db;
  //here I want to set the value $database from the DBOperations class to //$variable

有人可以帮忙吗?

关心弗朗西斯

1 个答案:

答案 0 :(得分:0)

您可以通过设置函数属性(如

)在functions.php类函数中定义它
public function doThings($variable) {

  $db = $this -> db;
   $this->db->database = 'dbname';
   // same for all variables if they are public
}