abstract class User
{
protected $content; // $content object will be assigned here
}
class Member extends User
{
}
abstract class Content
{
function create()
{
//create some content
}
}
class Text extends Content
{
}
class Image extends Content
{
}
Abstract User can extend to other classes based on rolls which are customizable. Each roll has a different permission to use Content class, and it’s child classes method. I can write manually each permission for each method, but I want to do that dynamically.
How can I do that?
UPDATE
On reading your question a second time, I’ve just thought of another way that is a better fit for what you need. Though php doesn’t support multiple inheritance, and traits require php >= 5.4, you might want to consider adding a second “generation” (another level of children). The abstract class contains only those methods that are available to all, preferably with
finalkeyword, or that only have 1 variation somewhere down the line (in which case, drop thefinalkeyword and override the member function).As I’ve said before, I’m not that good in explaining this stuff, So I went on ahead an put together an example of how your classes could look like:
See what I mean? Output:
If you’re using the latest version of PHP, you have traits for that. There’s a lot of ways you can check which class is invoking the memberfunction. The easiest, IMO, is by starting your methods by checking the value of
get_class($this);:echo’s
bar, so you can alter the abstract methods, and throw exceptions if necessary.The same construction can be used to overload certain methods, like this (terrible) example (tries to show/) shows: