I'm looking for a solution to restrict inheritance depth to one level in PHP. Similar to the 'friend' keyword in C++ I want to make sure that functionality of a specific class A could be used in class B, but not in class C which inherits from class B.
Did anyone already solve such a problem?
Declare methods as final in class B to prevent class C from overriding or extending them.
Related
So, I have two classes. I am wondering if I can have two types of objects of Class A:
normal objects of Class A,
objects of class A that inherit Class B's behavior whilst retaining their own,
without creating a third Class.
Is this possible?
Class A in its basic form does not have all the features of Class B, therefore extending Class B is not an option.
I am using PHP btw.
So, what I discovered was that if you want a certain behavior to be shared between different classes (horizontally), this is possible in PHP 5.4+ with the use of traits.
For example, you can define a trait called 'Driving' and various types of Driver actors can implement it.
The classes implementing traits will inherit all the properties and methods of the trait, and those properties and methods will appear as part of the class even when using reflection.
Here's a useful link that explains it well.
I have an abstract parent class P, and two concrete child classes C1 and C2. C1 and C2 extend from P.
In parallel to this, I have three interfaces, iP, iC1 and iC2. iC1 and iC2 extend from iP.
Obviously each child class should implement it's respective interface, but should the abstract parent class also implement its interface too?
Given that I will only be instantiating concrete classes, and these classes are bound to iP (by virtue of the interface inheritance hierarchy), I'm not sure if I really need P to implement iP?
Another alternative would be that I scrap the inheritance hierarchy on the interface side...
The sprit of interfaces is allowing decoupling, in other words, to separate the "what" from the "how". It's not a really good practice to hava and interface per class as you are stating in your question since it has no sense.
You can have in iP the signature of all the methods that your classes are going to implement, and abstract class P that partially implements the common methods that are to be reused in your child classes and the signature of the methods that you plan to write later in your child classes.
In this way, its enough that C1 and C2 extends your abstract class P since you are forced to implement all the methods declared in your interface and in your abstract class.
Hard to answer without knowing the business model.
But if you have an abstract class, my best guess is that you don't need interfaces for C1 and C2, just have one interface which P implements and also declares it's abstract method. Then the child classes extends P.
There are classes A and B both extend class C and there are some common methods in class C that are needed for both class A and B.
There are cases that it needs to invoke the method in class B from class A, which is at the moment achieved by creating an object of class B in class A. But, more and more methods are added into class C, so it is inefficient to create an object of class B inside class A due to the duplication.
My question's that what are the possible solutions to refactoring the classes so that they have better structure, e.g using object oriented, factory pattern, etc.?
Thanks in advance.
You could use Traits (only PHP >= 5.4.0).
If the methods needed by A and B can be factored out into a single responsibility then you might consider adding class D which is then used by class A and B. The Single Responsibility Principal (SRP) is the S in SOLID, which are guidelines for object oriented design.
You can find plenty of information about SOLID in general, and the application of SRP in PHP on Google. I gave a presentation to our local PHP Meetup Group on SRP which can be found online here: SRP Presentation
I am studying PHP and there is polymorphism example at the website i am studying at,
The Link for the Code:Pastebin, What i didn't understand is why Class b and Class c after those classes that have been extended with the function hellow when they are already contain this function, i know that this is how the polymorphism works, and i am not sure but i remember some one told me that 2 functions with same name can be in one class becouse each function has it own signature, but has i say i am not sure if can some one help me to understand it please i will be very thankful.
You are not talking about polymorphism here, but very simple inheritance. Multiple methods with the same name but different signatures are indeed possible in some OOP languages (like Java), but not PHP, where method names are unique in a class.
In your case, class b and c hello methods will simply replace (overload) the one already defined in a, which they extend.
But, from your b and c class, you may want to call a parent class method. That's exactly what a::hello($arg1, $arg2, date('Y-M-j')); does.
From a quick Google search and a the wikipedia article on Multiple Inheritance, which quotes:
Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass.
I understand that PHP doesn't allow multiple inheritance. What I can't find a clear answer on, however, is whether it allows more than one class to extend a superclass. Example:
class a {}
class b extends a {}
class c extends a {}
In terms of what I'm trying to do, I'm making an RPG and want a 'generic' character class to include all the methods and properties that make the template of a character. Then I want classes to include specifics for each type of character (warrior, mage, etc), such as stats modifiers and special attacks.
Is this possible?
Yes, it's perfectly possible. The entire purpose of inheritance is that multiple children can inherit common functionality from a parent.
Yes, multiple classes can extend the same class, per your code example.
This is the foundation of OOP. It's possible.
Yes, any class can extend a base class. In your example, it just isn't possible for class c to extend both a and b.
So in an adjusted example like the one above
class a {}
class b extends a {}
class c extends b {}
c could not get to properties of a ?