在构造函数中分配时从父 class 访问返回 null 的属性
Accessing properties from parent class returning null while assigned in constructor
我正在尝试从控制器 class process_loginController 内的 class adb 访问 属性 mysqli
,只是 $this->mysqli
returns null,而不是布尔值 true
(连接到数据库)。在 process_loginController.php
中,我有处理登录的脚本,但是当我登录时,我得到 Fatal error: Call to a member function prepare() on a non-object in cf.class.php on line 3
,即 if ($stmt = $this->mysqli->prepare("SELECT....")) {
。我在 cf.class.php 中为 $this->mysqli
做了一个 var_dump 并且它 return null
.
adb.class.php
class adb{
public $mysqli; // this properties cannot be accessed
public $db;
public $sql_details;
public $hello_there;
public function __construct() {
$this->mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$this->db = PDOManager::getInstance();
$this->hello_there = '123';
}
public function url(){ // this function can be accessed
$url = 'http://www.example.com';
return $url;
}
}
cf.class.php
class cf extends adb {
public function login($email, $password, $db) {
if ($stmt = $this->mysqli->prepare("SELECT....")) {
// some code here
}
}
}
controller_base.class.php
Abstract Class baseController extends cf {
protected $registry;
function __construct($registry) {
ob_start();
$this->sec_session_start();
$this->title_of_page();
$this->registry = $registry;
$this->registry->template->is_logged_in = ($this->login_check($this->mysqli) !== false) ? true : false; // this returns what should return so it's ok
}
abstract function index();
}
process_loginController.php
Class process_loginController Extends baseController {
public function index() {
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p']; // The hashed password.
if ($this->login($email, $password, $this->mysqli) == true) {
// Login success
header('Location: ../');
} else {
// Login failed
header('Location: index.php?rt=signin?error=1');
}
} else {
// The correct POST variables were not sent to this page.
echo 'Invalid Request';
}
}
}
我还尝试了一个更简单的方法:
hello.php
require 'model/adb.class.php';
require 'model/cf.class.php';
class hello extends cf {
public function hello() {
var_dump($this->mysqli); // should have returned true instead NULL
var_dump($this->url()); // this one is accessed and returns ok `example.com`
var_dump($this->hello_there); // should have been 123 instead is NULL
}
}
$conect = new hello;
归根结底,由于某些原因,这些属性无法访问,但函数可以访问,但我不明白为什么。
你永远不会打电话给你的 parent __construct()
。因此你的 mysqli 永远不会被创建,是的 null
.
我知道这不是代码审查。但是你应该永远不要使用public属性。就是不要
您还应该重构代码以使用依赖注入。现在,每个控制器都有不同的 mysqli object,而不是共享相同的资源。
你的方法也做了很多,他们做事,他们处理错误,他们回显输出给用户,他们做一些重定向,......保持愚蠢简单,并追求单一责任
我正在尝试从控制器 class process_loginController 内的 class adb 访问 属性 mysqli
,只是 $this->mysqli
returns null,而不是布尔值 true
(连接到数据库)。在 process_loginController.php
中,我有处理登录的脚本,但是当我登录时,我得到 Fatal error: Call to a member function prepare() on a non-object in cf.class.php on line 3
,即 if ($stmt = $this->mysqli->prepare("SELECT....")) {
。我在 cf.class.php 中为 $this->mysqli
做了一个 var_dump 并且它 return null
.
adb.class.php
class adb{
public $mysqli; // this properties cannot be accessed
public $db;
public $sql_details;
public $hello_there;
public function __construct() {
$this->mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$this->db = PDOManager::getInstance();
$this->hello_there = '123';
}
public function url(){ // this function can be accessed
$url = 'http://www.example.com';
return $url;
}
}
cf.class.php
class cf extends adb {
public function login($email, $password, $db) {
if ($stmt = $this->mysqli->prepare("SELECT....")) {
// some code here
}
}
}
controller_base.class.php
Abstract Class baseController extends cf {
protected $registry;
function __construct($registry) {
ob_start();
$this->sec_session_start();
$this->title_of_page();
$this->registry = $registry;
$this->registry->template->is_logged_in = ($this->login_check($this->mysqli) !== false) ? true : false; // this returns what should return so it's ok
}
abstract function index();
}
process_loginController.php
Class process_loginController Extends baseController {
public function index() {
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p']; // The hashed password.
if ($this->login($email, $password, $this->mysqli) == true) {
// Login success
header('Location: ../');
} else {
// Login failed
header('Location: index.php?rt=signin?error=1');
}
} else {
// The correct POST variables were not sent to this page.
echo 'Invalid Request';
}
}
}
我还尝试了一个更简单的方法:
hello.php
require 'model/adb.class.php';
require 'model/cf.class.php';
class hello extends cf {
public function hello() {
var_dump($this->mysqli); // should have returned true instead NULL
var_dump($this->url()); // this one is accessed and returns ok `example.com`
var_dump($this->hello_there); // should have been 123 instead is NULL
}
}
$conect = new hello;
归根结底,由于某些原因,这些属性无法访问,但函数可以访问,但我不明白为什么。
你永远不会打电话给你的 parent __construct()
。因此你的 mysqli 永远不会被创建,是的 null
.
我知道这不是代码审查。但是你应该永远不要使用public属性。就是不要
您还应该重构代码以使用依赖注入。现在,每个控制器都有不同的 mysqli object,而不是共享相同的资源。
你的方法也做了很多,他们做事,他们处理错误,他们回显输出给用户,他们做一些重定向,......保持愚蠢简单,并追求单一责任