Jump to content

Class dependency


snivek

Recommended Posts

I have been trying to learn Classes recently to take a more OO approach at my PHP.  I have noticed that a lot of other coders make static calls to other classes from classes.  i.e., They have a Image Exif class and in one of it's methods it calls a static method from a Utilities class.  This is done fairly often and with many varying methods.

This confuses me because IMO it kills the re-usability factor that is supposed to be associated with OOP.  If I have a class that makes calls to a bunch of external methods then I cannot use this class with other application without using those other classes, and those classes might not really be necessary in my new app.  Or I'd have to go through my potential large class and find all my calls and remove or change them.

Maybe I'm looking at this on to small a scale or just from the wrong angle.  Am I missing something?

I have had many little questions like this come up while trying to learn OOP.  If anyone could provide any links to a good write up on the approach and design of classes, I would really appreciate it!  I don't need to learn the CODE but more the thought/theory/approach.
Link to comment
Share on other sites

I'm still learning OOP and its terminology myself. Can you post some example of this code?

If I understand correctly, I'd say that "no man is an island" also applies to classes. In other words, if you are writing a large class that works with a database, you are going to use an existing database class instead of writing your own from scratch; thus, creating a dependency. There it is: many pieces working together to create the larger whole.
Link to comment
Share on other sites

good point. I for one tend to write classes that encompass all of the functions needed to complete the desired action. however, the point of functions (methods that are public) is to restrict rewriting of code. so if a class has a method that does a basic task, id call it.

it shouldnt be too difficult to write classes that dont really depend on other methods, if they do, you can put those methods in base class or function file if you're going to use those classes in different applications.
Link to comment
Share on other sites

That not really a good way to write a class, as you noted, it then makes the class depended on another class that may have nothing at all to do with the class calling it. A class should be independent, and the only time it shouldn't be, is when the class derives from an extended class that truly relates to it's parent.

There are some other exceptions, but those never relate to a single class. What I am saying is that a (drop in class), is very different from a application that uses many different classes, where a class or many classes are specific to an application. So what may be called unjustifiable in a (drop in class), will be justifiable in a (application specific class). Why, because it makes more sense to include, pass or call a static method or object, then to to create outside code blocks that might never be called and that are not related, but the most important reason is that you will then create a very big problem because your class will define methods and properties that will undoubtedly have to be written again in other classes. So sometimes you have to live with that trade off. So in closing, when writing a big application calling static methods from other classes is justified, not always, but sometimes it is the best approach for application specific logic.

printf
Link to comment
Share on other sites

I have a hard time trying to define what should be in a Class.  If I create a ImageFile class that contains some properties like name, hegiht, width, etc and some methods like resize and get_thumbnail.  Should I put something like get_exif in it?  Should I make EXIF it's own class?  And if I do, should I put a method in my ImageFile class that makes a call to the EXIF class?  Decisions, Decisions!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.