类中的变量 - 正确的方法

时间:2012-04-23 01:57:05

标签: php oop

此代码将输出相同的内容。我的问题是,这样做的正确方法是什么。第一种方法还是第二种方法?还是有更好的方法?我认为一个班级没有任何优势。

<?php
    class Client{
        var $id;
        var $email;

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

        public function get_email($db){
            $sql = $db -> prepare(" SELECT email FROM users  WHERE id = ? ");

            $sql -> bind_param('i', $this->id);
            $sql->execute();

            $sql->bind_result($email);

            if ($sql -> fetch()) {
                return $this->email=$email;
            }
            else
            return false;
        }
    }

    class Client_{
        public function get_email($db, $id){
            $sql = $db -> prepare(" SELECT email FROM users  WHERE id = ?");

            $sql -> bind_param('i', $id);
            $sql->execute();

            $sql->bind_result($email);

            if ($sql -> fetch()) {
                return $email;
            }
            else
            return false;
        }
    }
    ?>

的index.php

<?php
$Client = new Client(1);
$a = $Client -> get_email($db);

print_r($a);

$Client_ = new Client_();
$b = $Client_ -> get_email($db, 1);

print_r($b);
?>

3 个答案:

答案 0 :(得分:1)

在第二种方法中,实例化类是没有意义的,因为没有任何东西可以存储以备将来使用。因此,如果不同的数据存储在其他地方,最好使用“静态类”:

class Client_{
    static public function get_email($db, $id){
        $sql = $db -> prepare(" SELECT email FROM users  WHERE id = ?");

        $sql -> bind_param('i', $id);
        $sql->execute();

        $sql->bind_result($email);

        if ($sql -> fetch()) {
            return $email;
        }
        else
        return false;
    }
}

// And use it static way without instantiating first:
Client_::get_email( $arg1, $arg2 );

如果我要在这两个人之间做出决定,我将采取第一个决定。

我不知道你将如何使用这些课程中的任何一个但仍然对我而言更有意义的是存储$db并从外部提供$id并使$email为本地:

class Client{
    var $db;

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

    public function get_email($id){
        $sql = $this->db -> prepare(" SELECT email FROM users  WHERE id = ? ");

        $sql -> bind_param('i', $id);
        $sql->execute();

        $sql->bind_result($email);

        if ($sql -> fetch()) {
            return $email;
        }
        else
        return false;
    }
}

同样改变了这一行:return $this->email=$email;,也许我弄错了,但我觉得它没有意义。

答案 1 :(得分:0)

我认为第一个在这种情况下更“正确”,因为类名表明该类是数据库表的模型,这意味着表的大多数或所有更改都应该通过类抽象。通常,模型类的实例化将表示数据库表的一行,因此id是该类的成员是有意义的。

我还会通过构造函数将数据库连接传递给模型,或者以某种方式让它全局可用。

^只是我的2美分

答案 2 :(得分:0)

请停止使用var在php中定义类变量。它不再是4.x了。现在有publicprivateprotected

也就是说,Client的实例需要连接和标识符:

class Client
{
    protected $connection;
    protected $id;
    protected $email = null;

    public function __construct($connection, $id)
    {
        $this->connection = $connection;
        $this->id = $id
    }

    public function getEmail()
    {
        if ( $this->email === null )
        {
            $query = 'SELECT email FROM users WHERE id = ?';
            $statement = $this->connection->prepare( $query );
            $statement->bind_param('i', $this->id);
            if ( $statement->execute() )
            {
                 $statement->bind_result($this->email);
            }
        }
        return $this->email;
    }

} 

P.S。我实际上更喜欢PDO而不是MySQLi用于连接API。如果没有其他原因,那只是因为它在错误处理方面具有更大的灵活性。

相关问题