Object oriented programming, and related...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Object oriented programming, and related...

Post by mikusan »

Hi all, I am trying to understand what the heck am i doing, but not in the normal way where i show you my code and you tell me that i am dumb i forgot the semicolon but in a programming style way. :D

Here is the thing, myself i prefer to write code extremely modular, not only because i can reuse the code, but also because it's easier to read, and more professional, if not many other things i can say about it. Thing is that I often program with a bunch of includes, a main one called functions.php that contains all of the includes i will need and then all the functions that i can reuse anytime anywhere.

But, Someone brought up some talk about template parsers, and reminded me that mine, effective as it seems, is not written as a class like many examples out there, yes it's my original code, but i could turn it into a class if i wished, but at this point i ask myself why would i do that...

If i transform mmy template parser function into a class, extend it as many times as i wish i would not accomplish much more than i did with a simple function, i would only have to write more code, because i would have to create a new object of class class, and then go from there assigning here and there... a PIA if i must say. Then why do people bother with classes, could someone explain when i should use them, and why for that reason... Am i a bad programmer since i don't use classes in PHP?

Java makes things easier for you, you kinda HAVE to write classes, but with PHP i feel the freedom, i just hope that i am not using a bad way... since i have plenty of time and will to change.

Thanks...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

It all depends.

I like OOP, but I know that I don't follow through on it as often as I "should." I love design (software design, not artistic), but can get bored on implementing it. As a result I often end up with the boring bits implemented as "procedural programming" wrapped in OOP. This, I know, is useless. Eventually I get back to the code and refactor it properly, but....

If you have a single function that handles all your template parsing needs, then I wouldn't see much point to turning it into a class. However if you're single function is huge and long, it probably should be split up into helper functions and such. At this point it may or may not make sense to turn it into a class.

Basically, OOP was designed to help manage complexity and maintainability. If your code is simple and you find it easy to maintain, leave it alone. A quick test of maintainability, though, is to give the code to a programming friend and ask them to make a minor change. If they can't, you may to need to re-evaluate your maintainability. What a friend can't do today, you probably can't do in 6 months, if you leave it alone that long.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

So you are saying that if i have many functions that do the same thing "technically" or are "related" to each other in some way then i should organize them into a class... that brings up the next question, what if i took all my program apart and wrote it as a class with a bunch of extends... then technically it would function as my includes right now, perhaps i see that this way i would not always call all of my functions but only the ones i need that instant, i could see that as a bonus... but frankly editing a class based program seems much harder to me than a bunch of funcitons... correct me if i am wrong...

Now then this means that i am not OOPing though i would like to... then wat kinda style would mine be called?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well, it still depends. There are many "libraries" of functions out there. In some case it makes more sense to orgnize a collection of functions into a "classic" library than a class. However, if you find yourself using global variables or common data structures in most of the functions of the library, a class that encapsulates it may make more sense.

Whether is easier or not, depends on your design. A poorly design class system will be tough to update.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I think one of the best things you can do concerning object oriented design is get into patterns. The Book of Four:

http://www.amazon.com/exec/obidos/tg/de ... 201633612/

Another most excellent book to get into is the Refactoring book from the same publishers:

http://www.amazon.com/exec/obidos/tg/de ... 201485672/

Both books go hand in hand. Also, http://www.phppatterns.com will help translate some patterns into PHP code for you.

I have found OO design to be less focused on actual objects, and more about reusability. You can design using object oriented patterns and what not without using Objects. It just takes a bit of work before you can do it right.

OOP is a way to design software. Just because you use objects doesn't mean your programming is OO.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

The Gang of Four book is good. I found its intro section (the design of the editor) much more useful than the pattern descriptions. The way it talks through the design and why the different patterns were useful is one of the best arguements for OOP I've seen.

However I find "patterns" less useful in the context of teaching people about OOP. I feel there is a missing step, something to make people understand the why, not the how of OOP is needed, too. The GoF's intro partially does it, but is eclipsed by all the pattern stuff.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

mikusan wrote:So you are saying that if i have many functions that do the same thing "technically" or are "related" to each other in some way then i should organize them into a class...
This kind of thing does make a lot of sense. If you base your design on the RDD (Responsibility Driven Design) approach, then this is right in line with it. Basically, organize bits of code based on what it's responsible for, whether or not they are objects!
mikusan wrote: ...that brings up the next question, what if i took all my program apart and wrote it as a class with a bunch of extends...
This sounds as though you are getting dangerously close to what's called a God class. One where everything is thrown into it, normally including the general logic of the program. This is an extreme no, no. You also have to keep in mind method calls are around 10 times slower than function calls, and ops on referenced vars are around 2 to 3 times slower. That said, having everything work from some uber class is bad from the viewpoint of design and performance.
mikusan wrote: then technically it would function as my includes right now, perhaps i see that this way i would not always call all of my functions but only the ones i need that instant, i could see that as a bonus... but frankly editing a class based program seems much harder to me than a bunch of funcitons... correct me if i am wrong...
If what we are talking about is a God class, then you're correct. Everytime you make a change to that class, you're affecting the entire program and the likelyhood of foobar'ing something is extremely high.

On the other hand, if there are a collection of classes, or ADT's, or modules or functions, one only has to update or change the class, ADT, module, or function in question.

A perfect example is an airliner. A 777 is capable of carrying a number of different engines under wing. These different engines have different strengths, weaknesses, and performance characteristics. However, any of them can be changed out in a manner that is transparent to the pilot and the rest of the plane for that manner because how the different engines connect to the plane is through a consistent interface.

That said, a db connection object in your code should be the same way. You should be able to connect to any db on the planet by simply changing that object. Furthermore, how those objects interace with the rest of the code should be consistent. A mysql db connection object should look exactly like an oracle db object from the interface point of view. I hope that's clear. :evil:
mikusan wrote: Now then this means that i am not OOPing though i would like to... then wat kinda style would mine be called?


I wouldn't yet worry about that. However, I do suggest that you look into concepts such as Responsibility Driven Design (RDD), Object Oriented Design (OOD) (which in my opinion is an analog of RDD), and other things like Orthogonal Design.

Also, Patterns and Anti-Patterns both are very helpful. One thing to keep in mind though is that the use of Objects is NOT required for good design! However, you may want to be careful with that last statement as I've gotten myself into trouble with it. :twisted:

Cheers,
BDKR
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

x10 slower?? Where are you getting that figure from? My own benchmarks shown only a factor of 2.... (And some of that should go away with PHP5 :) )
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

nielsene wrote:x10 slower?? Where are you getting that figure from? My own benchmarks shown only a factor of 2.... (And some of that should go away with PHP5 :) )
I'm sorry! I squawked that out from memory, which didn't serve me correctly. Doh! You are right at around 2x slower. My benchmarks show the same thing.

Anyways, the data came from here.

http://php.weblogs.com/discuss/msgReader$537?mode=day

I got some of the performance figures munged up in my head when writing the earlier post, but there is the source for 'ya. :-) It's an interesting read. Another thing to keep in mind is that John Lim has done tons of work with his own class lib, ADOdb, and it's likely the fastest OOP php user land abstraction layer 'out there'.

Cheers and good catch,
BDKR
owen
Forum Newbie
Posts: 15
Joined: Fri May 30, 2003 12:40 pm

OOP in my view

Post by owen »

I aggreed with most what the previous posters have stated. But I have come agross alot of "unecessary OOP". In most cases having you program entirely in OOP results in a lot of extra "contructor" code just to get the page to work.

It's an option still, OOP does make you program better or easier to read. It's simply forces the progammer to follow a set of rules.

OOP classes work best when all the code is running at once, in a single memory space. And when the objects are totally independent or extended a "God" object.

OOP is bad when classes created peform the same functions as other classes. Like pages on a website - they just create html. So you end up with this horizontal object hierarchy of unessary classes.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'd definitely recommend experimenting with writing the function out as a class.

It may or may not be a better way to achieve your specific aim but I find myself that classes can really help to make code more modular, easier to understand and easier to maintain.

Once you start splitting things up into basic elements you usually wind up with lots of vars to pass between all the individual subfunctions. The ability to access class properties makes life much easier.

Inheritance is a very powerful feature which again can make code easy to maintain. Common code can be shared between whole trees of classes.

Take file related tasks for example. You could have a base class with some basic functions - path handling and error reporting for example - then extend that in one branch with file writing functions, and in another branch with file reading functions. You've now got elements of a file browser and file management/ftp in separate branches, all sharing some common code residing in the base class.

Finally, you can add some new childs on top of all that to create output for a specific site. For a new job, all you have to do is write a few new top-level childs.

I must admit I'm stlll in the middle of working all this out - I wouldn't know if this is an example of a "God" class which BDKR has warned about.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: OOP in my view

Post by BDKR »

I'll say first off that I'm notorious for NOT being an OO zealot. Just read some of the rants at the address below.

http://mgaps.highsidecafe.com/index.php?cat=4

That said, I agree with you for the most part. But I'm unclear on some of what you said.
owen wrote:I aggreed with most what the previous posters have stated. But I have come agross alot of "unecessary OOP". In most cases having you program entirely in OOP results in a lot of extra "contructor" code just to get the page to work.
When you say "constructor" code, that's unclear. Are you talking about the overhead of instantiation? Instantiation is an overhead we can't get around! However, you also have to remember that a class doesn't have to have a constructor.
owen wrote: OOP classes work best when all the code is running at once, in a single memory space. And when the objects are totally independent or extended a "God" object.
Running in a single workspace is how it normally works. If you're talking about distributed objects, then it's not a question of working best, but instead, working with how much overhead from things like interprocess communications, network traffic, semaphore acquisition, or whatever is needed.

And you don't have to have objects to have difffering processes work together. :-)

And what do you mean by "...extended a "God" object"?
owen wrote: OOP is bad when classes created peform the same functions as other classes. Like pages on a website - they just create html. So you end up with this horizontal object hierarchy of unessary classes.
Preach on brother!

OO is cool for the most part, but like you said, just over-used. Good design happens inspite of OO. However, we want to be careful that we don't knock OO, but instead OO over use.

Cheers,
BDKR
owen
Forum Newbie
Posts: 15
Joined: Fri May 30, 2003 12:40 pm

Post by owen »

by "constructor" code I mean...hmm. Alright you remeber when you just started programming and you code just think up something and then write it? Now you can't do that again because you have this enormous set of "must use" code that you have to add before you get to write what you really wanted to write in the first place. It happens to me alot. That what I mean by "constructor" code.

"God object" this is where the fun in OOP is :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well most good IDE's will write most/all of the "constructor" code (I think you mean a lot of the "boiler-plate" code used in cases -- the get/setFoo stuff).

Just thinking stuff up and writing it is one of the worst things you can do for any decent size project, with or without OOP. Proper design is fundamental.

"God" objects are an abuse and waste of OOP. Its a procedural programmings response to OOP, without under standing what OOP is about.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

nielsene wrote: Just thinking stuff up and writing it is one of the worst things you can do for any decent size project, with or without OOP. Proper design is fundamental.
Have you ever read some of the interviews over at Artima.com. Sure, it's a Java (and increasingly Python) shop, but the people that are interviewed are fantastic and talk about coding on a non-language specific manner.

Anyway, what I quoted above reminds me of some of the newer thoughts coming from much of the "agile software" gang. They don't really agree 100%. In some cases, they almost do support a kind of code from the hip style, however, I'm sure that depends on the size of the project and number of coders on the team.

Here is the interview with Andy Hunt and Dave Thomas. It's part of a series actually.

http://www.artima.com/intv/tracer.html

Here's a quote:
Bill Venners: In your book, The Pragmatic Programmer, you suggest that programmers fire "tracer bullets." What are tracer bullets? And what am I trading off by using tracer bullets versus "specifying the system to death," as you put it in the book.
Hope you like it.

Cheers,
BDKR
Post Reply