我有点问题。我有一个名为BaseModel的类。有一个pdo连接。现在我有一个名为TestModel的其他类,我扩展了BaseModel类。但是当我在pdo变量上创建一个var_dump()时,它返回null。我知道它的问题因为构造函数,但我该怎么做呢?我需要TestModel中的构造函数。但是whitout constcuter变量返回null。我已经尝试过白色parent::__construct()
,但页面加载无限。
这是我的课程
BaseModel
<?php
namespace App\System\MVC\Models;
class BaseModel
{
protected $config;
protected $connection;
public function __construct($config, $connection)
{
$this->config = $config;
$this->connection = $connection;
}
public function __destruct()
{
$this->config = null;
$this->connection = null;
}
}
?>
TestModel
<?php
namespace App\System\MVC\Models;
use App\System\MVC\Models\BaseModel;
class TestModel extends BaseModel
{
protected $config;
protected $connection;
public function __construct()
{
var_dump($this->connection);
}
public function __destruct()
{
$this->config = null;
$this->connection = null;
}
}
?>
请帮帮我。 感谢
抱歉英文不好。
答案 0 :(得分:2)
您永远不会将所需的变量传递给子类实例,因此您无法获得任何其他结果。对于初学者,使用正确的参数调用超级构造函数:
class TestModel extends BaseModel
{
// no need to redeclare the properties
public function __construct($config, $connection)
{
// pass the variables to __construct() in BaseModel
parent::__construct($config, $connection);
// some other initialization
}
// no need for destructor since the parent one is called
}
现在您可以像这样使用您的模型:
$obj = new TestModel($config, $connection);
var_dump($obj->getAllChildren()); // whatever operations that you want
但是每次使用模型时都必须传递$config
和$connection
,这是一种痛苦,因为你可以肯定。在这种情况下,您可能想要某种factory。你能做的最简单的就是这样:
class ModelFactory
{
const NS = 'App\System\MVC\Models';
private $config;
private $connection;
public function __construct($config, $connection)
{
$this->config = $config;
$this->connection = $connection;
}
public function create($class_name)
{
$reflection = new ReflectionClass(self::NS . '\\' . $class_name);
return $reflection->newInstance($this->config, $this->connection);
}
}
这将只允许您指定一次参数,然后您可以非常轻松地创建任意数量的模型实例:
$factory = new ModelFactory($config, $connection);
$obj1 = $factory->create('UserModel'); // creates new App\System\MVC\Models\UserModel
$obj2 = $factory->create('GroupModel'); // creates new App\System\MVC\Models\GroupModel
这应该会让事情变得容易一些。请记住,通常模型没有连接,它们只有(元)数据,然后另一个对象负责执行查询。看看repository and data access object patterns。
答案 1 :(得分:0)
在实例化子类时,不会自动调用父构造函数,而在您的情况下,无法为父构造函数提供依赖项,因为它们未提供给子构造函数。
你的课程应该像这样重新实现:
<?php
namespace App\System\MVC\Models;
class BaseModel
{
protected $config;
protected $connection;
public function __construct($config, $connection)
{
$this->config = $config;
$this->connection = $connection;
}
public function __destruct()
{
$this->config = null;
$this->connection = null;
}
}
?>
<?php
namespace App\System\MVC\Models;
use App\System\MVC\Models\BaseModel;
class TestModel extends BaseModel
{
/* You don't need to redefine them as they are inherited from the parent
protected $config;
protected $connection;
*/
public function __construct($config, $connection)
{
parent::__construct($config, $connection);
var_dump($this->connection);
}
/* This is also inherited from the parent
public function __destruct()
{
$this->config = null;
$this->connection = null;
}
*/
}
?>