When to use OOP

Hi all, I’ve began learning OOP, it’s not as difficult or confusing as I thought it would be, it’s kind of straight forward when you understand the basics. Just have a few questions:

(1) When should I use OOP rather than procedural? For example, if I was building somebody a blog site, pretty basic, would it be worth using OOP?

(2) Is it a good idea to use prepared statements over mysql_query when using OOP? Still can’t seem to get the hang of prepared statements!

Thanks in advance :smiley:

Any time you develop a piece of software that has more than one “object” of reference in its design, you should consider OO.

A blog could have many objects:

  • User
  • Administrator (extends User)
  • Content (abstract)
  • Blog (extends Content)
  • Category (extends Content)
  • StaticPage (extends Content)
  • Comment (extends Content)
  • Menu (extends Content)
  • Media (e.g. images)
  • MediaManager
  • Theme
  • Database
  • Etc…

Prepared statements have nothing to do with OO. They’re just a more organized way of generating queries. Ideally, the marjority of your OO application shouldn’t even be concerned with queries or databases… if you implement a leading OO pattern, e.g. n-tier or MVC, you’ll have a specific layer of your application that handles the database, and the rest of your code just uses your “Data Access Layer” (DAL, in n-tier) or Database “Model” (the M in MVC) to handle data as objects.

Cheers!

Pretty much any site that uses PHP could benefit from OOP.

1.) A blog, with article queries, comments, etc would benefit from OOP

2.) This can depend on the nature of the queries.

Hey chaps, so would the best way be to build a class for each object and put all the objects into one class. Apologies, still learning and trying to grasp the basics of OOP :slight_smile:

Look into MVC frameworks before deciding on your class architecture.

I’d tell you to check out my framework I’m developing (QiPHP) but it’s not ready for public consumption yet. :smiley:

So go check out Zend, Cake, and CodeIgniter.

They each have established design patterns and methodologies to follow.

If you want to do something completely custom, then you’ll need to establish your layers before your classes. At minimum, you’ll need a Data Access Layer (DAL), a Business Logic Layer (BLL), and a Presentation Layer. Your DAL should handle database connection, inserts, updates, and selects for each table in your database. Your BLL should handle each of the primary business operations of your site (login, create a post, create a comment, moderate comments, etc.), and your presentation layer should focus on interfaces that users will interact with, calling the BLL to execute operations, which will call the DAL for persistence of data.

Cheers.

most of the time I use OOP, becouse it’s easy to maintain, and once you made classes they are easy reusable for other projects.

Hey chaps, cheers for the replies. So is OOP pretty much the same as procedural? As in the sense that you still use functions such as date, echo, arrays, etc. I know you use the -> a lot but apart from that, is there a great deal to it?

I’m imaging it as something completely different and very hard to get to grasps with. Or is it relatively easy to pick up?

If you are not going to go with a ready-made framework, start your new site like you normally would. As soon as you copy some code from somewhere else, or get that deja-vu feeling as you type then you are probably dealing with something that would be a good candidate for one of your first classes - and the smaller it is the better.

The objects derived from your classes should only do one thing, but do it really well.

Connecting to your database is always a likely candidate, and seeing as you are asking about mysql/mysqli take the time to look at PDO. Try extending that to do what you want in terms of database access.

Hey Cups, yeah I’ve been feeling that for sure so I’ve just had a quick go at my first attempt at OOP, would you mind taking a look and it’s up to scratch with OO. It’s all working nicely which I guess is the main thing :slight_smile:

class General {
	
	function linkTidy($data){
	$this->data = preg_replace("/[^a-zA-Z0-9\\s]/", "", $data);
	$this->data = str_replace(" ", "-", $this->data);
	$this->data = strtolower($this->data);
	return $this->data;
	}
}

I feel quite proud of myself :lol:

Hey.

What you’re doing looks like a perfect place for a Intercepting Filter, but don’t read that yet. :slight_smile:

Whenever I think about how I’m going to write a class, I always endeavour to keep it as generic as possible. Doing so makes the resulting object reusable, this lets me drop it into another application for example without having to write it again.

At the moment, your example is pretty specific.

What if we had something like…


$filter = new FilterChain;
$filter->addFilter(new CallbackFilter('strtolower'));
$filter->addFilter(new CallbackFilter('ucfirst'));
echo $filter->apply('FOO!'); #Foo!

We can now change how the code works at runtime, allowing much more flexibility.

Can you see that? :slight_smile:

Hey bud, thanks for your message. Yeah think I get the gist of your example, just finding this OO way of programming really tough to get my head around, it’s proving a bit of a nightmare. I’ve managed to pick up procedural really well and progressing at a good pace. Are there any other advantages to OO apart from reusable code? Can you recommend any good books regarding OO? Apologies for the many questions lol :slight_smile:

Maybe you can tell by the varied replies you have to this thread, but we are all pretty much talking about applying principles.

The principles of OOP as we have individually learned them, are likely to be different to each of us. They are personal. How we describe them toe each other, and to you in this particular medium - and you ask a particularly pertinent question, what book should I buy?

To be glib, I suppose the answer is, it depends on how you learn.

But, if you are like me, to really use OOP properly you have to invest an awful lot of time and effort - we are talking years - and I am really just a novice at this point.

you ask me to criticize your class, I mean I read it twice and I still cannot understand what it is supposed to do. Anthony implies it does too much.

Now both of use can start reeling off principles that your class has broken in each of our expericences - and I guarantee that neither of us will agree (I know I have drunk beer with him) - except for one thing, it could be done better.

But that is us applying imaginary solutions to your imaginary or implied problems.

It is your journey to make, certainly we can show you signposts only.

My best advice consists of two things:

Take a day off, go the biggest city nearest to you. Find the biggest bookstore, in that city and spend the entire day browsing OOP books 'till you find the one that you feel you understand - be prepared to buy a book whose sample code is not PHP ( the best ones seem to be Java ).

Search this forum for “OOP books”.

Hey bud, thank you so much for your detailed reply and for taking the time to write it. Indeed, you make a lot of sense in the fact different people likely think of OOP in slightly different ways. I’ve been doing a bit of research and splashed out £30 on a book, should be here tomorrow, so excited, how sad I am! Thank you again for your message, you have tier up a lot of loose ends for me. If your ever in Devon, I owe you a pint :smiley:

Hi Kriss, I thought this was worth adding.

Hey Ant, cheers for the message! I was looking at one of those books but went with PHP Object - Oriented Solutions by David Powers in the end, but thinking I’ll also get the one you mentioned. Just waiting for the post now…probably won’t be until 4pm or something :shifty:

Enjoy your morning, I’m off down to the pub for a big breakfast :stuck_out_tongue:

You … :x