2016-05-21 20 views
1

我在,我想註冊用戶可以通過一個單獨的類訪問與他們的帳戶一切的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

發佈您的代碼了。 – gvgvgvijayan

+0

我已經更新了我的問題,其中也包括我的代碼... –

回答

1

解析性能的實際影響應該可以忽略不計。然而,單純從設計的角度來看,應拆分此成多個類和使用組合物,或Composite Pattern

他複合模式描述了一組對象的是在方法一樣被處理一個對象的單個實例。組合的目的是將對象「合成」到樹結構中以表示部分 - 整體層次結構。實現組合模式可以讓客戶端統一處理各個對象和組合。

所以,與其特質,事物像一個名爲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(); 

     // ... 
    } 
} 
+0

請問您可以用一個例子多說明一下... –

+0

好的,請檢查以上:) – Will

+0

感謝您的快速回復,但有一個問題仍然存在 我沒有在每個時間的所有對象的要求。有時候我需要個人資料信息,有時候會有書籍請求等......那麼爲什麼我要爲所有包含的類創建對象... –