0

Procedural PHP works for me, but when I try to code like I do in objective C I cannot seem to get the functions to work. I think it has something to do with my variable scope and the functions not returning any values. Where am I going wrong with my code here?

      <?php 

      require_once("constants.php");

      class Main {

public $menu;
public $connection;

function connectToDatabase() {
    //connect to database & check connection
    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " .      mysqli_connect_error(); }
}

function queryDatabase($select, $from) {
    $result = mysqli_query($connection,"SELECT $select FROM $from");
}

function closeDatabaseConnection() {
    mysqli_close($connection);
}

function displayMenu() {

    //connect to database & check connection
    connectToDatabase();

    //get menu data
    queryDatabase(*, pages);

    //construct menu data
    echo '<ul class="mainNavigation">';
    while($row = mysqli_fetch_array($result))
        {
            echo '<li><a href="' . $row['filename'] . '.php">';
            echo $row['menu_name'];
            echo '</a></li>';
        }
    echo '</ul>';

    //close connection
    closeDatabaseConnection();
}

function displayFooter() {

}

function getUser() {

}

function userLogIn() {

}

  } 
?>
4

4 回答 4

1

你应该使用 $this->connection 而不是使用 $connection,并像这样运行它:

$Db = new Main();

$Db->connectToDatabase();

如果要访问方法内的属性,请确保使用 引用它$this->prop,而不仅仅是 $prop。如果方法是静态的,请self::prop改用。

$this指类的当前实例化。::如果方法/属性是静态的 ( DBConnection::connection DBConnection::connectToDatabase())/ ,您将使用范围解析运算符 ( )

另外,我将类“Main”重命名为 DBConnection 之类的其他名称(PHP 不像 C 那样使用主要方法),然后:

$Db = new DBConnection();
$Db->connectToDatabase();

因为在 PHP 中,虽然您可以在一个名为 connection.php 的 PHP 文件中拥有一个类,但具有一个 Main 类,如果您有另一个遵循相同模式的类,那么您将有两个 Main 类。

于 2013-04-04T18:37:41.817 回答
1

如果你想在它自己的类中使用方法或变量,你必须放在$this->它前面。例如,这connectToDatabase();将成为这个$this->connectToDatabase();

于 2013-04-04T18:37:49.420 回答
0

这是我经常使用的 mysqli 连接类。它可能无法回答您的问题,但您可以研究它,它应该可以帮助您更好地理解这个概念。无论如何,它对我有用。

class DBConnection  
{       
    protected $mysqli;
    private  $db_host='localhost';
    private  $db_name='';
    private  $db_username='';
    private  $db_password='';

    public function __construct() {
        $this->mysqli = new mysqli($this->db_host,$this->db_username,
        $this->db_password, $this->db_name) or die($this->mysqli->error);

        return $this->mysqli;
    }

    public function getLink() {
        return $this->mysqli;
    }

    public function query($query) {
        return $this->mysqli->query($query);
     }

    public function real_escape_string($str) {
        return $this->mysqli->real_escape_string($str);
    }

    function __destruct(){
        $this->mysqli->close();
    }
}

然后当您需要连接到另一个类中的数据库时,请在构造函数中调用它,如下所示:

public function __construct(DBConnection $mysqli) {
    $this->mysqli = $mysqli->getLink();
}
于 2013-04-04T22:06:42.543 回答
0

这门课有各种各样的问题,所以我将重点关注几个关键点,也许你可以从那里修复它,而无需我们完全重写

首先,考虑一下:

public $connection;

function connectToDatabase() {
    //connect to database & check connection
    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " .      mysqli_connect_error(); }
}

你声明一个类变量$connection,没关系。但是在您的函数中,您分配了一个局部变量而不是访问类变量。在一个类中,您可以使用 $this 调用所有类成员。所以函数应该是:

function connectToDatabase() {
    //connect to database & check connection
    $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " .      mysqli_connect_error(); }
}

现在,其他方法尝试调用函数,但它们需要做同样的事情:

function displayMenu() {

    //connect to database & check connection
    // this should just check to see if $connection is populated. if not then call this method
    $this->connectToDatabase();

并且此方法不访问类 var 进行连接:

function queryDatabase($select, $from) {
    $result = mysqli_query($this->connection,"SELECT $select FROM $from");
}

这几个例子应该给你访问类方法和变量的基本概念。您可以提出具体问题,我们可能会更新,但现在这应该让您继续前进。

于 2013-04-04T18:39:48.733 回答