特质和 classes:担心 class 的长度

Traits and classes: worried about length of class

我正在开发一个 Web 应用程序,我希望注册用户可以通过单个 class(会员)

访问与其帐户相关的所有内容

喜欢:

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 classes 中使用特征,每个特征有 500-600 行。现在我担心要编译的 class 长度。我在单个 class 中使用了 6-7 个特征(或者将来可能更多),并且 class 代码变成了大约 5000 行。 class 编译期间对性能有任何影响或遵循这种方法有任何缺点。

我喜欢的风格:

trait Profile {
    ...
}

trait books {
    ...
}

trait Services {
    ...
}

等等,主要的class是:

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;
...
}

如果我走错了路,你能给我建议最好的方法吗? 谢谢

对解析性能的实际影响应该可以忽略不计。但是,纯粹从设计的角度来看,您应该将其拆分为多个 classes 并使用 composition,或 Composite Pattern:

he composite pattern describes that a group of objects is to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

因此,像 "profile" 这样的东西应该是 class 的对象而不是特征,称为 MemberProfile,用这个特定成员的信息实例化。例如,在 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();

        // ...
    }
}