Help me see the point in OO PHP

Associate
Joined
7 Dec 2007
Posts
302
Location
Derbyshire, Tibshelf
Right, for my degree my tutor is saying I need to code my php game using objects... but I can't see what the hell is the point when it is just me coding it?

From what I've read everywhere...
Objects = slower, more to code, great for groups as each person can code a module...

But what the hell is wrong with just including libraries of functions instead? I still don't understand the concept of an OBJECT.

Ok... users need to login... login process in a user object? users need to be able to build stuff... building objects per building?

I also don't get how the hell this all ties into a database... wtf is an object?!

I really don't see why it shouldn't be done all using functions
 
An object usually maps onto a relational database quite easy for most things. One person object = one row in person table. When starting to use inheritance and composition , look into orm techniques and orm libraries which will save a lot time.


Objects are just instance of the class.
 
Last edited:
Wouldn't it have been better to search for a tutorial on OO rather than asking a forum?

There is nothing /wrong/ with using libraries and doing it in a procedural style, but the object oriented style of writing code is just as valid (if not arguably more so) than procedural and, therefore, is important for you to learn about!

Personally, I prefer OO because it just makes sense to me to look at an application or section of an application from the point of view of the substantial parts that make it up. For example, when you visit a page as a logged in user, have an object that represents all of your details. As has been said, objects also map well onto a relational database and allow you to organise interaction with that database into logical files/classes. Yes, there might be more overhead to instantiating classes and calling methods, but OO is also an excellent way to logically organise your code to make it easier to revisit and augment in the future. You also, hopefully, don't end up with functions.php that weighs in at 2MB!
 
There's a couple of things I'd say on OO programming, the first is it's hard to see the point until you actually start doing it. The main reason I ifnd myself using the OO approach is when I want to store lots of similar but not identical data sets. So, for example in a game sense you might have, say a class called weapon, which might store things like reload time, weight, clip size etc... and a set of associated functions so SetReloadRate() etc... now the beauty about OO is that say one of your weapons has a "feature" that isn't generic (say a right click action), then you can just create a sub class of weapon that'll inherit all the functions, and data structure of the weapon class but where you can add in an function to say do something if the player right clicks.

Hope that helps a bit
 
It sounds to me like you don't want to use OOp for the sole reason that you don't understand it.

In our company framework, there is a single file that is procedural - the index.php entry point. All this does is initialise several core objects and pass off execution to a routing one - from that point on every single thing is an object.

As far as speed goes, I've never noticed any slowdown - in fact - by only loading the files/objects you need (using autoload naturally) you often have less to parse than if you include loads of utility functions into the global namespace.

Anyway, this is all academic (sorry!) as your tutor has decreed that you must use objects. Have a look at something like the Zend Framework for a look at how they structure things.
 
One of the dangers of OOP is that it's incredibly easy to misuse it when you don't understand it properly, and it can take a while to get to the point where you do have a good understanding of it. Looking back, the first OO PHP applications I wrote were horrible tangles of quasi-OO and procedural code.

PHP isn't the best language to learn OOP in, as it doesn't really impress upon you the significance or usefulness of it – mostly because of its weak/dynamic typing system, and that its libraries are, for the most part, procedural rather than OO. If you have to learn it in PHP though, it'd be very valuable to look at real life examples of it, so you can see how it's used in practice, as philjohn said.
 
Last edited:
Back
Top Bottom