login.php中的getdata()內的getuserkey()和$mysqli都在另外兩個文件,
1.怎樣把兩個不同的class(sql.php和key.php)在另一個class(login.php)上面使用?
2.但是呼叫getuserkey()又需要呼叫db,那在login.php和key.php該怎樣修改?
sql.php
<?php
class db {
function __construct() {
$this->conn();
}
private function conn() {
$mysqli = new mysqli('localhost', 'root', 'root', 'test');
return $mysqli;
}
}
key.php
<?php
class key {
public function getuserkey() {
$key = $mysqli->query("SELECT * FROM key")->fetch_assoc()['key'];
return $key;
}
}
login.php
<?php
class login {
public function getdata() {
$key = getuserkey();
$data = $mysqli->query("SELECT data FROM data WHERE key='$key'");
if($data = $data->fetch_assoc()) {
return $data;
} else {
return FALSE;
}
}
}
iT邦幫忙MVPfillano提到:
composer
iT邦幫忙MVPfillano提到:
樓煮
4. 使用類別前,請先用new產生一個>>>實例<<<(如果學過其他物件導向語言,應該會知道這個)
7. login.php:跟key一樣阿,要有地方存$mysqli,才能用它。>>>沒有產生class key的實例前,你沒辦法呼叫他的getuserkey()方法<<<
原來new產生那個叫>>>實例<<<,而用getuserkey()也要用>>>那個實例<<<
特別感激fillano指點!!
已修改:
key.php
private $_sql;
private $_key;
function __construct($sql, $key) {
$this->_sql = $sql;
$this->_key = $key;
}<?php require_once 'sql.php'; require_once 'key.php'; require_once 'login.php'; $sql = new db(); $key = new key($sql); $login = new login($sql, $key);
例:e又用到d,d又用到c,c又用到ab,ab才是獨立;除了合成一行,可以在class弄嗎? $a = new a(); $b = new b(); $c = new c($a, $b); $d = new d($a, $b, $c); $e = new e($a, $b, $c, $d); ...
$db = new db; $login = new login($db, new key($db));
<?php
class db {
function getDb(){
return 'Run';
}
}
class key {
function __construct(db $db)
{
$this->db = $db;
}
function go()
{
return $this->db->getDb();
}
static public function instance(db $db)
{
return new self(new db);
}
}
$key = key::instance(new db);
echo $key->go();
<?php
class db {
function getDb(){
return 'Run';
}
static public function instance(){
return new self;
}
}
class key {
function __construct(db $db)
{
$this->db = $db;
}
function go()
{
return $this->db->getDb();
}
static public function instance(db $db)
{
return new self(new db);
}
}
$key = key::instance(db::instance());
echo $key->go();
static public function instance(db $db)
function getAddressOfSingleUser(){
//這只是個範例,沒事別搞那麼長。
}
class GetAddressOfSingleUser {
//就說了這只是個範例,寫那麼長的話………其實感覺也挺爽的!
}
ryo89589提到:
login.php中的getdata()內的getuserkey()和$mysqli都在另外兩個文件
tkdmaf提到:
最後一個是有人明明回答,卻又把別人的答案換個文字敘述重寫一次
<?php
class db {
function getDb(){
return 'Run';
}
static public function instance(){
return new self;
}
}
class key {
function __construct(db $db)
{
$this->db = $db;
}
function go()
{
return $this->db->getDb();
}
static public function instance(db $db)
{
return new self($db); //改成這樣才對
}
}
$key = key::instance(db::instance());
echo $key->go();
<?php
class login {
private $_sql;
private $_key;
function __construct(db $sql, key $key) {
$this->_sql = $sql;
$this->_key = $key;
}
static public function instance(db $sql, key $key)
{
return new self($sql, $key);
}
public function getdata() {
$key = $this->_key->getuserkey();
$data = $this->_sql->query("SELECT data FROM data WHERE key='$key'");
if($data = $data->fetch_assoc()) {
return $data;
} else {
return FALSE;
}
}
}還是回一下好了。
總之...加油吧