OOP Question - Extend vs New Class
I'm just looking for opinions on my situation in relation to oop. I'm a beginner at the moment. Basically in my site I have files, photos and I also have messages.
Therefore I currently have a "message" class and a "file" class. Both of these currently extend a class called "general" which has several methods, all of which both of these classes make use of hence the extension of the general class in each.
However the both of these classes now need to make use of another method called "getAuthor" which I haven't included anywhere just yet. The thing is I also have another class called "photo" which DOES NOT extend the general class (yet) however it DOES need to make use of the "getAuthor" method.
What is the best way of handling this situation? Perhaps by creating a new class called "Author" and instantiating that class inside the other classes wherever it is needed? The other option seems to be putting getAuthor in the general class and then extending the general class in the photo class but considering the fact that the photo class uses none of the other methods in the general class this seems bad practice?
When deciding whether to use a class or extend a class, it can be helpful to ask whether the class "is a" something or whether it "has a" something. In the case of an author, both a message and a photo "has a" author, so I think you're on the right track with creating an Author class that both the Message and Photo classes can use.
Ideally, once implemented, you might end up writing code this way:
$author = new Author('Joe Shmoe');
$author = new Author('Jane Smith');
What Jeff is (astutely) suggesting is often referred to as "composition", in case you want to try some searching on that topic. You inherit (extend) when class A "is a" class B, and you use composition when class A "uses" class B. A classic example of composition is passing a database object (such as a PDO or MySQLi object or one from your own custom class) to a data model object of some kind so that the model can access the database via the DB object.
I would put setAuthor in the general class and extend the photo class with general if that method is required for photos also. Essentially you want to put everything in general class that belongs to all three object classes.
Thanks very much guys. That's very clear and now I understand it perfectly
I suppose the best approach would be to make a general folder inside my class library folder containing all general classes that are used in many places and then instantiate certain classes as and when they are needed inside other classes i.e. "new General_Author()" etc.
@faithless - Those were my original thoughts too however the photo class doesn't use any of the other methods in the general class so it seems bad practice because I'd be including a lot of methods that the class doesn't need to implement at all.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)