I am working on a web application in which I want registered users can access everything related to their account via a single class (member)
Like:
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
and so.
I am using traits in my PHP classes with having 500-600 lines in each trait. Now I am worried about the class length to compile. I have used 6-7 traits (or can be more in future) in a single class, and class code becomes around 5000 lines. Is there any effect on performance during compilation of class or any drawback of following such approach.
Style which I am following:
trait Profile {
...
}
trait books {
...
}
trait Services {
...
}
etc., and the main class is:
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;
...
}
If I am on a wrong way, could you please suggest to me the best way to accomplish the same? Thanks.
The actual impact on parsing performance should be negligible. However, purely from a design standpoint, you should split this up into multiple classes and use composition, or the 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.
So, instead of traits, things like the "profile" should be objects of a class called MemberProfile
, instantiated with information for this particular member. Inside Member
, you could access something from the profile via $this->profile->getName();
or $this->profile->name;
, for example.
Here's a quick example:
<?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();
// ...
}
}