简体   繁体   English

扩展的pdo连接返回null

[英]extended pdo connection returns null

I have a problem. 我有个问题。 I have 2 classes (RouteController and BaseController). 我有2个类(RouteController和BaseController)。 In the class BaseController i have my pdo connection included, and working. 在类BaseController中,我包含了我的pdo连接,并且可以正常工作。 And in the class RouteController i have extend the BaseController and i want that the connection also works in the RouteController class. 在RouteController类中,我扩展了BaseController,并且我希望该连接在RouteController类中也起作用。 But if i make a var_dump() it returns NULL. 但是,如果我做一个var_dump(),它将返回NULL。 How do i can make that it works? 我如何使它起作用?

Index.php: index.php文件:

<?php

ini_set('display_errors', true);
error_reporting(E_ALL);

require_once 'App/Config.php';
require_once 'App/Controllers/BaseController.php';
require_once 'App/Controllers/RouteController.php';
require_once 'App/Controllers/DatabaseController.php';

$connection = new DatabaseController($config['database']['host'], $config['database']['user'], $config['database']['pass'], $config['database']['name']);
$connection = $connection->Connection();

new BaseController($connection);
new RouteController;

Here are the classes: 这些是类:

DatabaseController DatabaseController

<?php

class DatabaseController
{
  private $host;
  private $user;
  private $pass;
  private $name;

  public function __construct($host, $user, $pass, $name)
  {
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
    $this->name = $name;
  }

  public function Connection()
  {
    try {
      $this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name, $this->user, $this->pass);
      $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
      $this->db->exec("SET CHARACTER SET utf8");
      return $this->db;
    } catch(PDOException $e) {
      die($e->getMessage());
    }
  }
}

BaseController BaseController

<?php

class BaseController
{
  protected $connection;

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

RouteController RouteController

<?php

class RouteController extends BaseController
{
  public function __construct()
  {
    var_dump($this->connection); // Return NULL
  }
}

I need some help please, thanks. 我需要一些帮助,谢谢。 Sorry for my bad english. 对不起,我的英语不好。

try with this code: 尝试使用以下代码:

<?php

class RouteController extends BaseController
{
  public function __construct($connection)
  {
    parent::__construct($connection);
    var_dump($this->connection); // Return NULL
  }
}

$myObject = new RouteController($con);

UPDATE: 更新:

<?php 

class BaseController
{
  private $connection;

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

  public function getConnection() {
      return $this->connection;
  }
}

class RouteController extends BaseController
{
  public function __construct($connection)
  {
    parent::__construct($connection);
    /* some other construct code */
  }
}

$test = 'hello';
$myObject = new RouteController($test);
var_dump($myObject->getConnection());

this work 这项工作

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

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