2011-03-07 188 views
1

我在PHP中遇到全局變量問題。我只包含以下數據mysqli的配置文件:PHP訪問全局變量的問題

$mysqli = new mysqli("localhost", "user", "pass", "db"); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

我有另外一個文件下面的類:

class user{ 

function username_exists($username){ 
    global $mysqli; 
    if ($stmt = $mysqli->prepare("SELECT username FROM users WHERE username=?")) { 
     $stmt->bind_param("s", $username); 
     $stmt->execute(); 
     $stmt->store_result(); 
     $count=$stmt->num_rows; 
     $stmt->close(); 
    } 
    return ($count > 0 ? true : false); 
    } 

    ... 
    some more functions 
    ... 

    } 

現在這個工作罰款,但在我previous question on SO,有人告訴我,訪問全局變量是一種不好的做法,就像我在上面的課程中所做的那樣。所以,我試圖通過全局變量在構造函數中,以下列方式:

private $mysqli; 
     function __construct() 
     { 
     global $mysqli; 
     $this->mysqli = $mysqli; 
     } 

    function username_exists($username){ 
    //global $mysqli; 
    if ($stmt = $this->mysqli->prepare("SELECT username FROM users WHERE username=?")) { 

,我得到以下錯誤

Fatal error: Call to a member function prepare() on a non-object in...(line number) 

能否請你告訴我什麼問題它以及如何解決這個問題?謝謝。 編輯:對不起__construct的拼寫錯誤。這裏輸入的只是錯誤,而錯誤不是因爲這個。

+0

爲什麼ü寫'專用$ mysqli的;'最後的代碼塊 – diEcho 2011-03-07 13:09:10

回答

1

我認爲這個問題是你misstyped __construct試着改變你的__cuntruct爲構造正確的名稱。

username_exists中的global也是無用的。

你也應該寫一個構造函數,採用變量作爲參數,避免使用全局完全地:

class User { 
    var $mysqli; 

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

    [ ... some functions ... ] 
} 

你必須創建你的對象是這樣的:

$myuser = new User($mysqli); 
$myUser->prepare(); 
+0

感謝您的答覆。對不起,我輸錯了構造,但那不是問題。我試着用你描述的方法,我得到2個錯誤,第一個警告:'缺少用戶:: __構造()的參數1,在......中調用,第二個錯誤與我在原始文章中提到的相同。你能看看我的原始文件中的配置文件的代碼。再次感謝。 – Jay 2011-03-07 16:23:53

+0

當您創建對象時,您必須傳遞您的$ mysqli變量。我想你應該閱讀有關對象的PHP文檔:http://php.net/manual/en/language.oop5.basic.php在我看來,你並不真正瞭解你在做什麼。順便說一句,我更新了我的帖子,關於如何創建對象 – krtek 2011-03-07 16:29:17

-1

刪除功能username_exists()中的global $mysqli;,這沒有任何意義。

global $mysqli;不是必需的/沒有意義,因爲您希望此變量存儲引用連接的對象的上下文中。

3

呃......在你的構造函數中有global有點勝利。考慮通過它作爲參數__construct($mysqli)

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

你想在這裏做什麼叫做dependency injection

-1

變化__cuntruct()__construct()

+1

上應該是'__construct' – bradenkeith 2011-03-07 13:12:07

1

你的構造是沒有得到所謂的因爲它根本不是構造函數

__cuntruct 

應該是

__construct 
+0

爲什麼downvote ...我真的想知道我錯在哪裏 – 2011-03-07 13:20:57

+0

可能有人試圖取消我的upvote,所以他們的答案等級更高...你的答案是正確的,並且是最快的。 – bradenkeith 2011-03-07 13:24:18

+1

我正面臨着這個問題,有人downvote並沒有迴應他爲什麼這樣做? Downvote,這是好的,但要小心留下評論 – 2011-03-07 13:31:50

1

書面真的沒有什麼上,因此其他人試圖鼓勵你做的代碼。

function __construct($mysql_handler){ 
    $this->mysql = $mysql_handler; 
} 

這是傳遞參數到構造的對象範圍。當你創建你的對象的實例時,你會傳入mysql句柄。

$mysqli = new mysqli("localhost", "user", "pass", "db"); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$u = new User($mysqlli); 

然後你應該可以在屬性本身上調用mysqlli成員函數。

你的構造也被拼錯。它只能使用魔術方法名稱__construct()正常工作。

1

夫婦的事情。如果您將__cuntruct更改爲__construct,我認爲它可以正常工作。

你還在使用username_exists函數內global聲明。爲什麼不在構造函數中傳遞$mysqli變量?

function _construct($mysqli) { 
    $this->mysqli = $mysqli; 
} 

,那麼你必須在類中沒有全局。

+0

感謝您的答覆,但拼寫錯誤只是在這裏打字,對不起。我已經解決了這個問題,並且還試圖在構造函數中傳遞變量,它仍然會給出相同的錯誤。如果你看到我的配置文件代碼在上面,我需要改變什麼嗎?謝謝。 – Jay 2011-03-07 14:04:39