Jump to content

Need guidance on OOP


sib

Recommended Posts

Hi,

 

First time poster and a bit of an OOP noob, so please go easy on me :)

 

I need some advice and/or guidance on OOP. I am building a personal website at the moment and I have a mysql wrapper which is an abstract class containing common mysql scripts such as inserts/selects/deletes etc. I have then created another class which extends the mysql wrapper class and contains the same select/insert/delete methods calling them by using parent::method_name(). This child class will be used for all of the pages on the website which need to connect to a mysql db. To display the results from the query, another class is called which extends the above child class and contains methods specific to the page such as the html output from the query.

 

So, in other words, the structure is:

 

mysql_wrapper -> common_db -> page_class

 

Apologies if I have not explained this clearly, but if you do know what I mean, would this be advisable?

Link to comment
Share on other sites

Hi Sib,

 

It is usually a good idea to keep the code segmented by their function and what you suggest does that. How refined you make this depends on how big the project is and the relative skills of the programmers.

 

So, could there be a better way of handling your project? Possibly. But it is really hard to tell without getting into at least a basic analysis.

 

I like the idea of using a wrapper around your DB classes, that way you can swap in another database if need be.

 

... Not that I've ever seen this done in real life!

 

:D

 

That said, I would suggest leaning much more toward encapsulation rather than inheritance. When extending classes, you run the risk of creating dependencies down the road that could lead to major headaches. So to sum it up:

 

Use encapsulation and php interfaces rather than inheritance.

 

Stefan

Link to comment
Share on other sites

Hi Stefan,

 

Many thanks for the suggestions, it is really appreciated. I understand now that stacking up classes is perhaps not the best way of doing things, so I've been doing some reading on interfaces, but not exactly sure how I would use this to relate two classes together using an interface.

 

For example, I have two classes: the mysql_wrapper and a page class (which extends the wrapper and can access methods from it). How would I create an interface to do the same or achieve the same functionality? From what I have read, an interface is simply a blueprint to structure classes that implement the interface. Would this mean that in my interface I would have to lists all of the methods from the mysql_wrapper and all methods from the page class? If so, would this not create repetition amongst the different classes that implement this interface?

 

Once again, if I have my wires crossed I sincerely apologise, but if you have any more information on how this can be accomplished, please let me know.

 

Once again, thanks for your help and guidance.

 

Sib.

Edited by sib
Link to comment
Share on other sites

Hi,

 

Interfaces will not help you with code repetition, they are there to create a consistent structure in your classes. So for example, in your project you can say that all data access classes have to implement the 'data access interface' where in that interface, you create the shells for the various methods you want in every data access class.

 

This way, anyone using you classes will know that they can always expect certain methods. For example, you might define a method called: connect_to_database() in your interface. And any classes that use that interface will have to create/define (fill in the guts) of the method.

 

Interfaces become more important for larger projects, where you have multiple programmers working. Interfaces are a tool to keep the code structure consistent.

 

Bottom line: Base classes and inheritance are more or less useful for infrastructure code only - think frameworks. If you are creating business logic, I would not be looking to create a deep class hierarchy - that is to say, use inheritance.

 

Stefan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...