2015-06-20 37 views
2

我开始一个新的Web项目。我对处理用户帐户有一些疑问。我有一些基本的面向对象的知识,但新的PHP。正确处理类似但不同类型的对象,如用户帐户

我的场景非常熟悉。将有两种不同类型的帐户。客户和公司。他们将有一些共享和特定的属性。 在数据库中,我将使用三个表来存储用户数据。一个用于共享属性。其他人将获得有关用户类型的特定信息。

shared_data

+---------+-----------------------+---------------+----------+----------+--------+ 
| user_id |   email   | user_name | password | type | active | 
+---------+-----------------------+---------------+----------+----------+--------+ 
|  1 | [email protected] | Super Company | secret | company |  1 | 
|  2 | [email protected]  | Bold Company | cetres | company |  1 | 
|  3 | [email protected]  | John Doe  | retsec | customer |  1 | 
|  4 | [email protected]  | Jane Doe  | setrec | customer |  1 | 
+---------+-----------------------+---------------+----------+----------+--------+ 

company_only_data

+---------+-----------------+------------------+ 
| user_id | company_address | person_in_charge | 
+---------+-----------------+------------------+ 
|  1 | Berlin   | Steven Seagal | 
|  2 | Budapest  | Chuck Norris  | 
+---------+-----------------+------------------+ 

user_only_data

+---------+--------+--------------+ 
| user_id | gender | last_shoping | 
+---------+--------+--------------+ 
|  3 | male | never  | 
|  4 | female | yesterday | 
+---------+--------+--------------+ 

I D不想重新发明轮子。由于用户处理是网络编程的重要步骤之一,我认为我会找到有关该主题的更好资源。我读了很多文章,包括“抽象类和接口”,但没有一篇是清楚的。

我的计划是建造类似下面

class UserManager 
{ 
    private $user_id; 

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

    public function getData() 
    { 
     //Return joined data of shared_data table 
     //and second table (based on type of account) 

    } 

    public function deactivate() 
    { 
     //Update shared_data set active=0 WHERE user_id = $this->user_id 
    } 

    public function login() 
    { 
     //... 
    } 

    public function logout() 
    { 
     //... 
    } 
} 

class CustomerManager extends UserManager 
{ 
    public function getShoppingData() 
    { 
     //Return results of shopping_data table for $this->user_id 
    } 

    //...some other customer related methods 
} 

class CompanyManager extends UserManager 
{ 
    public function getSalesReport() 
    { 
     //Work with results of sales_data table for $this->user_id 
    } 

    //...some other company related methods 
} 

我在正确的轨道上或做错了什么?

你可以建议我一些有据可查的链接开始或建议我的方法吗?

+0

您能告诉我们为什么要尝试从用户管理从头开始......你可以使用框架或者CMS ......你不需要重新发明轮子 –

回答

0

您应该使用一个接口来设置常用的方法和属性。通过使用接口,您可以确保您的所有类都具有您的应用逻辑需要的最少的一组方法和属性(如登录,注销,commonDetails等)

+0

一些解释的例子会很棒。 – Nilambar