6

根据我阅读的演练内容,我收到了不同的回复,

我定义了一个有 2 个函数的类。

我希望这两个函数都可以访问数据库凭据

目前,除非我将变量复制并粘贴到每个函数中,否则此代码不起作用。

我在这里做错了什么?

<?php
class database  {
function connect()  {
var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
                    }

function disconnect()   {
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
    mysql_close($con);
                        }
            }
?>
4

6 回答 6

12

这个块:

var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;

应该在 之前function(),而不是在里面;但仍在class定义之内。

添加显式可见性是一种很好的形式;私人开始:

class database  {
    private $username="my_username";
    private $servername="localhost";
    // etc. etc.

然后,函数将它们称为:

$this->username;
$this->con;
etc.

理想情况下,您将希望构造函数传入这些凭据:

private $servername;
private $database;
private $username;
private $password;
private $con;

function __construct($host, $user, $password, $dbname)
{
    $this->servername = $host;
    $this->username = $user;
    $this->password = $password;
    $this->database = $dbname;
}

更理想的是,了解PDO

于 2012-06-14T13:40:31.407 回答
3

要访问对象属性,您需要使用 $this->property_name

 $this->con = mysql_connect($this->servername,$this->username,$this->password);

类代码如下所示:

<?php
class database  {
    var $username="my_username";
    var $servername="localhost";
    var $database="my_DB";
    var $password="An_Awesome_Password";
    var $con;

    function connect()  {

        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
    }

    function disconnect()   {
        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
        mysql_close($this->con);
    }
}
?>
于 2012-06-14T13:39:58.843 回答
3

撇开代码样式不谈,您需要在类方法之外但仍在类内部定义变量。就像是:

class database {
    var $username = "my_username";
    // etc.

    function connect() {
        // connect code
        // $this->username == "my_username"
    }
}
于 2012-06-14T13:41:14.053 回答
2

您必须使用函数外部的变量并使用构造破坏函数,它们将确保您正确打开和关闭连接

class database  {
    private $username = 'username';
    private $servername = "localhost";
    private $database = "my_DB";
    private $password = "An_Awesome_Password";
    private $conId;

   public function __construct(){
        $con = mysql_connect($this->servername, $this->username, $this->password);
        $this->conId = $con;
        //..........
    }


    public function __destruct(){
        mysql_close($this->conId);
    }
}
于 2012-06-14T13:42:23.583 回答
2

为类属性和方法分配其范围是一种很好的做法,可以是“公共”、“受保护”或“私有”,而不是使用“var”。此外,类属性在类内分配,但在任何函数之外(也称为“方法”)。这是你的类重构:

class database  {
    private $username="my_username";
    private $servername="localhost";
    private $database="my_DB";
    private $password="An_Awesome_Password";
    private $con;

    public function connect()  {
        if (!$this->con) {
            $this->con = mysql_connect(
                    $this->servername, $this->username, $this->password);

            if (!$this->con)  {
                die('Could not connect: ' . mysql_error());
            }
        }
    }

    public function disconnect()   {
        if ($this->con)  {
            mysql_close($this->con);
        }
    }
}
于 2012-06-14T13:46:14.593 回答
0

变量只能在创建它们的范围内调用。

如果在函数中创建变量,它只能在该函数中使用,但在梯形图上定义的越高,可用的变量就越多。

全球公私功能

于 2012-06-14T13:50:56.107 回答