Page 1 of 1

How would you turn this into OOP?

Posted: Thu May 14, 2009 7:23 am
by Hightower
Hi folks,

I'm pretty new to the OOP game, and need some (major) advice. I currently have an active project and I want to rewrite it in an OOP manor. The thing is, I don't know how to split it down (i.e. what classes, what functions within each class etc).

Can anybody give me a push start on this? Here's the details:

It's a PHP front-end to a Squid server, using a MySQL database for data storage. The Squid server is running as a whitelist filter, and my PHP app allows staff to log in and add sites to the whitelist. The features of it are:

::: Login (LDAP Auth)
::: Logout

EDITING FEATURES
::: Add new site (normal)
::: Add new site (hidden)
::: Delete site (normal)
::: Delete site (hidden)
::: Get a list of links for one site, and add to db
::: Add a new admin user
::: Delete an admin user
::: Add a new subject (subjects used to categorise sites for searching features)
::: Delete a subject
::: Add a new type of site (again, used to categorise sites)
::: Delete a type of site

SEARCHING FEATURES
::: Search by title
::: Search to get ONLY image sites
::: Search to get ONLY sites added today
::: Search by subject
::: Search by area

REPORTING FEATURES
::: Show access logs
::: Show activity logs

The site is up and running now, and works fine - I just to want make it OOP for my own learning and don't know where to start (i.e. as stupid as it sounds, what class do I write first and what functions should this class have).

Any help appreciated - if you need any more info let me know and I will provide it.

Many thanks, :)

Re: How would you turn this into OOP?

Posted: Thu May 14, 2009 8:38 am
by crazycoders
First thing i may tell you is that OOP is used to centralise data and features together, not functionnalities. Functions are the result of your application toying around with the different objects that represent the information you manage. At some very high level, you may transform EVERYTHING in OOP, but it often makes everything more complex.

PHP is a language that supports OOP but it is still a scripting engine that runs a function called MAIN and it runs in a linear fashion. You may divert from that fashion by creating objects that will manage your processing but i wouldn't recommend it.

What i do recommend is to first transform your data access to an object representation which is what objects/classes are mainly for, abstract a concept and represent it in memory. Editing a page is not an object, the page is an object and you have methods in it that may allow you to edit that page.

Common objects you may want to represent in OOP are:

Users, Permissions, Groups and in your case, sites, links, subjects/categories, pages, typeof<something>, logitems, etc.

After that, you may want to automate and render OOP a little more by changing your control flow into objects but that is debatable. I prefer the linear fashion several times before creating objects to manage the flow of operations of your site. I think this decision must be done on your side, some will tell you to go for it, others will not!

If you have any other questions i might be able to give you more info, just send them in!

Cheers

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:08 am
by Hightower
At the risk of sounding like a complete idiot.... I'm confused now.

I thought I knew the point of OOP, now I'm just completely lost once again. :cry:

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 7:18 am
by crazycoders
What are you confused about?

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 8:11 am
by Hightower
crazycoders wrote:What are you confused about?
Well, I was thinking that OOP was to create a class, lets say in my example, that deals with editing sites.

So this class might have a function to add a site, delete a site, and move a site.

Then I might have another class to deal with reporting, and user auth.

That's what I was under the impression that OOP was about.

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 8:35 am
by crazycoders
Well OOP is used to represent real life concepts into a model that describes it's properties and methods that make it act. For example:

- Properties of a car object would be it's top speed, its weight, its color
- Method of a car object would be to move, turn wheels, shift to another gear, start, stop, etc....

The same concept can be applied to your objects... An object for your site could be a "site". Sites are defined by properties such as name, address, theme color maybe? Actions that you can do to your site is save it's information to the database, delete it from the database.

If you wonder how you'd manage all the sites well you can model up an object that represents your software or your server. That object would have a method called addSite and removeSite...

Another concept that is important to grasp with OOP is that events are also something used by OOP but sometimes, it creates more problems than it solves so i would not use this in a scripted language because of the countless ties it creates. If you are interrested in more information relative to this event concept, search for the observable pattern.

Appart from that i can't really help you more unless you ask more specific questions.

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 8:48 am
by Hightower
No no, that's great. Your latest post helped me a lot (I think haha) :D

Is it a crime from a programmers point of view to NOT use OOP, or is it a matter of opinion and preference?

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:05 pm
by alex.barylski
Is it a crime from a programmers point of view to NOT use OOP, or is it a matter of opinion and preference?
Depends who you ask. I spent almost 15 years programming not using OOP. Then I started learning it and probably did it wrong for close to another 10 years. :P

Now when I look at procedural code, I want to cry because it's often worse than it's OO equivelant. If you are a passionate developer and care about code quality, than OOP is certainly the way to go. You can write elegant code in procedural or assembly, but you would do even better after a few years experience with OOP.

Programming using objects forces you to re-think how you solve problems. You tend to think more about the architecture, design and interface, as opposed to the algorithm, development and implementation, when following procedural programming.

Cheers,
Alex

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:09 pm
by Eran
I spent almost 15 years programming not using OOP. Then I started learning it and probably did it wrong for close to another 10 years.
8O
That would mean that you are programming close to 25 years?

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:12 pm
by crazycoders
You know pytrin, there were computers 25 years ago, it's possible :P

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:21 pm
by Eran
Yes, but somehow I figured Alex was around my age ...

Re: How would you turn this into OOP?

Posted: Fri May 15, 2009 3:51 pm
by Christopher
Hightower wrote:Is it a crime from a programmers point of view to NOT use OOP, or is it a matter of opinion and preference?
It is "a matter of opinion and preference" to a small group of starting PHP programmers. It is only an conversation in PHP because the other major languages do not support procedural programming. Among language designer and professional programmers is was long ago settled that OOP is the way to build software. Remember that procedural/structured programming is a subset of OOP.

In fact it is such old news that you don't hear procedural vs OOP much any more. The conversation has moved on to how OOP can be enhanced by functional programming (closures, anonymous functions/classes, lambdas, etc.).