我正在开发一个Web应用程序,我希望注册用户可以通过单个类(成员)访问与其帐户相关的所有内容
像:
example.com/controller_class/action_name
example.com/member/my-profile,
example.com/member/edit-profile,
example.com/member/my-orders,
example.com/member/mybooks,
example.com/member/my-book-requests,
example.com/member/my-notes,
example.com/member/my-notes-requests
等等。
我在PHP类中使用特征,每个特征中有500-600行。现在我担心要编译的类长度。我在一个类中使用了6-7个特征(或者将来可以更多),类代码变成大约5000行。在编写课程时是否对表现有任何影响或遵循这种方法的任何缺点。
我关注的风格:
trait Profile {
...
}
trait books {
...
}
trait Services {
...
}
等等,主要类是:
require_once 'traits/trait.Common.php';
require_once 'traits/trait.profile.php';
require_once 'traits/trait.books.php';
require_once 'traits/trait.services.php';
require_once 'traits/trait.notes.php';
require_once 'traits/trait.Account.php';
class MemberController extends LoggedUserController {
use Common, profile, books, services, notes, Account;
...
}
如果我走错路,你能否告诉我实现同样目标的最佳方法?感谢。
答案 0 :(得分:1)
对解析性能的实际影响应该可以忽略不计。但是,纯粹从设计角度来看,您应该将其拆分为多个类并使用组合或Composite Pattern:
复合模式描述了一组对象的处理方式与对象的单个实例相同。复合的意图是将对象“组合”成树结构以表示部分整体层次结构。通过实现复合模式,客户可以统一处理单个对象和组合。
因此,“profile”之类的内容应该是名为MemberProfile
的类的对象,而不是traits,而是使用此特定成员的信息进行实例化。在Member
内,您可以通过$this->profile->getName();
或$this->profile->name;
访问个人资料中的内容。
这是一个简单的例子:
<?php
require_once 'MemberProfile.php';
require_once 'MemberAccount.php';
class MemberController extends LoggedUserController
{
public $profile;
public $account;
public function __construct()
{
$memberId = $_GET['memberId'];
$this->profile = new MemberProfile($memberId);
$this->account = new MemberAccount($memberId);
}
public function display()
{
$accountBalance = $this->account->getBalance();
$fullName = $this->profile->getFullName();
// ...
}
}