同时调用 parent 和 child 构造函数
Calling both parent and child constructors
我的一些 classes 在 parent 和 child 中都有构造函数。我怎样才能 运行 两个构造函数?
Parentclass:
include 'c:/wamp/www/mvc/include/connect.php';
class Database
{
protected $mysqli;
protected $exc;
function __construct(mysqli $db)
{
mysqli_set_charset($db,'utf8');
$this->mysqli = $db;
}
<?php
Child class: (登录类)
<?php
class Login extends Database {
private $username;
private $password;
function __construct(mysqli $db, $username, $password)
{
parent::__construct($db);
$this->setData($username, $password);
$this->getData();
}
function setData($username, $password)
{
$this->username = $username;
$this->password = $password;
}
function getData()
{
$result = $this->mysqli->query("SELECT * FROM anvandare WHERE anvandarnamn = '$this->username;' AND losenord = '$this->password'");
$count = $result->num_rows;
if($count>0)
{
return true;
}
else
{
throw new Exception("Username or Password incorrect. Please try again");
}
}
LoginController.php
<?php
//LoginController
if($_POST)
{
if(isset($_POST['submit']) AND $_POST['submit'] == "login")
{
$username = $_POST['username'];
$password = $_POST['password'];
try
{
include '../model/Login.php';
$login = new Login($db ,$username, $password);
if($login == TRUE)
{
session_start();
$_SESSION['username'] = $username;
header("Location:../index.php");
}
}
catch (Exception $exc)
{
echo $exc->getMessage();
}
}
}
我尝试在 child 构造函数中调用 parent::__construct($this->mysqli);
,但不知何故它不起作用。
它可能不起作用,因为父构造函数需要传递 mysqli
的实例。不过,以下签名应该有效:
require_once __DIR__ . '/Database.php';
class Login extends Database
{
function __construct(mysqli $db, $username, $password)
{
parent::__construct($db);
$this->setData($username, $password);
$this->getData();
}
}
我的一些 classes 在 parent 和 child 中都有构造函数。我怎样才能 运行 两个构造函数?
Parentclass:
include 'c:/wamp/www/mvc/include/connect.php';
class Database
{
protected $mysqli;
protected $exc;
function __construct(mysqli $db)
{
mysqli_set_charset($db,'utf8');
$this->mysqli = $db;
}
<?php
Child class: (登录类)
<?php
class Login extends Database {
private $username;
private $password;
function __construct(mysqli $db, $username, $password)
{
parent::__construct($db);
$this->setData($username, $password);
$this->getData();
}
function setData($username, $password)
{
$this->username = $username;
$this->password = $password;
}
function getData()
{
$result = $this->mysqli->query("SELECT * FROM anvandare WHERE anvandarnamn = '$this->username;' AND losenord = '$this->password'");
$count = $result->num_rows;
if($count>0)
{
return true;
}
else
{
throw new Exception("Username or Password incorrect. Please try again");
}
}
LoginController.php
<?php
//LoginController
if($_POST)
{
if(isset($_POST['submit']) AND $_POST['submit'] == "login")
{
$username = $_POST['username'];
$password = $_POST['password'];
try
{
include '../model/Login.php';
$login = new Login($db ,$username, $password);
if($login == TRUE)
{
session_start();
$_SESSION['username'] = $username;
header("Location:../index.php");
}
}
catch (Exception $exc)
{
echo $exc->getMessage();
}
}
}
我尝试在 child 构造函数中调用 parent::__construct($this->mysqli);
,但不知何故它不起作用。
它可能不起作用,因为父构造函数需要传递 mysqli
的实例。不过,以下签名应该有效:
require_once __DIR__ . '/Database.php';
class Login extends Database
{
function __construct(mysqli $db, $username, $password)
{
parent::__construct($db);
$this->setData($username, $password);
$this->getData();
}
}