Stephen Craton said: > I've been reading up on object oriented programming in PHP for a while now > and I just don't get what's the use in using it. It supposedly makes it > faster, but I don't really see how in any of my scripts. What's the > advantage of OOP anyway, and why are so many people using it now? What > does it have to offer then just creating files full of functions to > include later like I've always done?
Good question. OOP is mysterious until you get it, then you'll want to write everything in objects. Show a man how to use a hammer, and everything looks like a nail :-) One use is to organize loose functions. For example, we have a user/group administration section. Each user is represented with a user object. With the user object I put functions inside which do things like "Give me your group ID," "Give me your permissions," "Give me your name." So rather than writing a function for each of these and squirreling it away into an include file, I organize it into a user class (and squirrel it away into an include file :-) Same with groups. So I wanted to produce a list of users in the group. I ran something like this (pseudocode): $group_obj = new Group (1); foreach ($group_obj->get_group_list() as $member_id) $user_obj = new User (45); $user_obj->get_name (); // Displays "John" The equivalent standard programming is: function get_member_name($id) $SQL = "select name from members where id = $id"; return $name function get_group_list ($group_id) $SQL = "select member_id from groups where group_id = $group_id"; return $array_of_ids foreach (get_group_list(1) as $member_id) get_member_name ($member_id); // Displays "John" A bit messier. OOP lets each class worry about getting its own data. With this organization you can think more clearly about writing the business logic. You can build each object to "do" whatever you need it to (Tell someone your name when asked, tell someone your blood type when asked, donate blood when asked), and then go back and build business logic to orchestrate each action into an application "User, who are you? User, what is your blood type?" "User, donate blood."). So my use was for organization. In this example, OOP was actually slower (don't know who told you it'd be faster; I sure haven't found that to be true) and so eventually I'll revert to ordinary functional code. For each time I fetch the member name, the user's object must be built up and torn down. Am I doing something wrong? Perhaps. But it tells me OOP isn't right for any and every situation. Not everything is a nail. Other uses include extension. For instance, I might see a class I like and will download it. Say I want to impliment it but with a few customizations; I can extend that class. In the ordinary function world, if a new version of a library comes out I'd have to reimpliment those changes into the library again, but with a class I don't much care which changes are where (to some extent). For example, we modified an ordinary menu-building system to be object-oriented so we could publish it back to the developers. We then extended it to do a permission check using the user object above like this: get menu items extension: for each menu item, is the user allowed to see it? If not, remove it from the list show menu items I can now publish that code without the custom permission check and it'll work for anyone. It also helps developers segment their work. For a grand example, take a look at the Java classes, which are organized in a tree-like fashion. The code at the top is generic and it gets more specific as you descend the tree. Inheritence is king for Java, and there's very little code rewrite (if the developer knows what he's doing). There are a number of other reasons to use OOP, but speed should not be the first consideration. Perhaps there are situations in which it is faster than standard functions but I haven't see them, or perhaps you've really got to tweak it. Instead, use OOP for a more organized way to build a program. That's my layman's understanding of OOP for laymen like myself. Hope it helps! CD -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php