duanluanlang8501 2014-02-05 15:34
浏览 23
已采纳

将变量转化为类'

So I've learnt a lot about PHP, and I started with the basics even if some of it is deprecated. Now I've got the knowledge down, I'm re-writing everything I've done to use PDO and class'. Now I've never used either but I'm slowly (slowly, slowly) understanding and getting my head around it. What I'd like to ask, is if I'm doing this right, if not where am I going wrong?

I basically have this function:

function user_exists($username) {
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
    return (mysql_result($query, 0) == 1) ? true : false;
}

And I want to turn it into a class:

class user {
    function exists($username) {
        $username = $username;
        $query = $db->query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
        $results = $query->fetchAll(PDO::FETCH_ASSOC);
    }
}

Am I on the right track, should I be making the variables in that function (method) private to the "exists" method?

I plan to make a "users" class, with methods such as exists, banned, admin, ect. I think this is what I should be doing?

Thanks if anyone answers this.

  • 写回答

2条回答 默认 最新

  • dongpu9852 2014-02-05 15:54
    关注
    class User {
        public $name;
    
        private $db;
    
        function __construct($name) {
            $this->name = $name;
    
            $this->db = get_me_a_db_connection();
        }
    
        public function exists() {
            $query = $this->db->query('SELECT COUNT(user_id) FROM users WHERE username = "' . $this->name . '"');
            $results = $query->fetchAll(PDO::FETCH_ASSOC);
    
            return some_code_that_extracts_the_count_from_$results;
        }
    }
    

    But: 1) $db should not be a class property. It should at least be in a factory. So you extract it in a factory:

    class Factory {
        public static function db() {
            return get_me_a_db_connection();
        }
    }
    
    public function exists() {
        $query = Factory::db()->query(...);
        ...
    }
    

    This way you don't pollute your domain models with infrastructure code that much.

    2) By that much above I mean you can achieve total decoupling between infrastructure code (database connection, querying) and your domain models. There are tons of libraries out there doing just that. I'd start with Laravel's Eloquent.

    3) PDO queries are safe if you use prepared statements. All the libraries like the ones I linked to above use prepared statements. As a noob I would recommend you start learning by studying one of them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败