When should a class be used?

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
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Agreed. All of your points are valid.

My primary point was that procedural code by itself is not necessarily evil. Remember the acronym PEBKAC? Problem Exists Between Keyboard And Chair. Most programming problems can be traced to the programmers themselves.

Few languages have constructs that are truly evil. In C, for example, goto is much maligned. The truth of the matter is that goto, used properly, is not a bad thing. In fact in some cases it can aid in the clarity of the code. Would I use goto? Hell no. I don't even use goto when I am using variations of BASIC. BTW - If you are running Linux like I am and want to see a cool variation of BASIC look at Gambas.

I think that all programmers need to look at and learn OOP. Find out how to write proper objects. Perfect their understanding of the system. Then decide when to use them.

I have written very clean and easy to read procedural PHP that is solid and performs well. Could I have done the same with objects? Absolutely. Objects do have some very worthwhile features that there is no parallel to in the procedural world.

I too learned PHP objects after I did other OOP languages. They were C++, Delphi, Java and then PHP and C#. Having been through these other languages, I found PHP extremely comfortable. It is easy to craft objects in PHP and PHP5 is hands down better than all the versions before it. The overloaded get and set methods rock. I abuse autoload like you wouldn't believe. And the new object features make PHP5 a joy to use.

BTW - In my PHP framework I descend all objects from a base object called TObject - ala Delphi. A TObject has basic error handling, logging, overloaded get and set to implement properties and other features. From there you have TPersistent which is a TObject that is allowed to stream to almost any type of media. I have written custom streams and a TStream object that can stream to just about anything so an objects state can persist between page loads because they are stored to disk files or shared memory, etc.

The properties are handled through overloaded get and set method that verify that the property exists and controls retrieval and assignment. When you define a property in an object you tell it what type it is. The object obeys that type and returns all data in that type. The beauty of that is a $_POST variable that is stored in a property will be forcibly converted to the type that the object supports. If the object is a Float for example, you can then say Property->AsString() and you get a string version. If you say Property->AsCurrency you get a formatted currency string. And before you ask - there is a TI18N object that handles internationalization.

One of the other cool features is crude enums. You can define an enumerated list that is named and has a value. Then you can create an enumerated property and tell it the enum type and it will validate the enumeration whenever there is an assignment into the property.

And that is just the core functionality. The layers above it are cool too. I do device independent rendering of objects so an edit box can spit out HTML (with javascript validators) or XUL or just an XML representation of the object.

I also have an import function that is used to declare your intent to use an object. If you say:

Code: Select all

import('core.TObject');


The framework verifies the existence of the object and marks it for late binding. If you use it, good. If you don't it is never even loaded into memory. You can also say

Code: Select all

import('core.*');
import('Renderer.HTML.*');
And it dutifuly registers all objects as potentially used. The benefit is that you need not carry the baggage of the object, it only is loaded IF you create an instance of that object.

I also use static classes as containers to avoid using globals. So the TApplication static class contains configuration information about the current web application and you never need to instantiate it. It is just there for your use.

So as you can tell. I really like objects but I use them when I feel the time is right.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

damn thats a hell of a framework - especially for someone who isn't as OOP-ish as MCGruff. What the hell do you build with that thing? A lot of the PHP apps out there could use something like that.

Thing that sucks for me is that at work we are running PHP 4.something with no hope of going to 5 anything soon. I want overloading! And Privacy! We are working oh talking the bosses into a bottom up rewrite, however, using only good OOP practice as the app we are currently working with was started 5 or 6 guys ago by guys with little to know programming experience and lots of, uh, weed. Yea. I've seen some astoudingly bad code.

Plus we are not even running MySQL 4 yet - we are still in 3 somethign. It kills me - everytime i design a good query it ends up using something that's not implemented yet. (but thats a different story)
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

From Dictionary.com
pragmatist

n 1: an adherent of philosophical pragmatism 2: a person who takes a practical approach to problems and is concerned primarily with the success or failure of her actions.
I always select the tools and methodology based on the task at hand. In some cases I am doing bug fixes for a client or extending an existing application so I will tailor my code to fit the existing app - minus the bugs or poor design that is. If I write a fresh application, I have to consider the tools that the company uses and what they are familiar with so I again have to shoehorn my development style to my clients. If I do it for myself, it is PHP/MySQL for web stuff and Delphi for Windows apps and there will be OOP involved in all of it.

I am OOP-ish though. I have many years of programming under my belt and my OOP development start in the late 80s. I alse have a decade of Delphi development which is solely (in my case) OOP so I use it all the time.

My framework is under development and the test bed is a production application for a start up. Being a partner in the start up, I have demanded that the framework is opensourced when it is stable.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Being a partner in the start up, I have demanded that the framework is opensourced when it is stable.
glad to hear it!
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

smpdawg wrote:My framework is under development and the test bed is a production application for a start up. Being a partner in the start up, I have demanded that the framework is opensourced when it is stable.
I'll be looking forward to seeing this framework of yours too. Please make an announcement when you do open source it.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

smpdawg wrote:a TStream object that can stream to just about anything so an objects state can persist between page loads because they are stored to disk files or shared memory, etc.
In case you're interested, there's some discussion on creating a port of Hibernate for php here.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Thanks for the info. I am going to read up on it and see if it would go well with what I am doing.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

So I strolled over to sitepoint and saw this. http://geocities.com/tablizer/oopbad.htm

I know you've seen this one McGruff, what was the outcome? Is the guy delusional? I stopped looking when I saw him compare OOP to communism...
McGruff wrote:
smpdawg wrote:a TStream object that can stream to just about anything so an objects state can persist between page loads because they are stored to disk files or shared memory, etc.
In case you're interested, there's some discussion on creating a port of Hibernate for php here.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I think he's managed to convince himself, at least.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

That is what happens when you work in a vacuum.

I have worked for quite a few companies that do over $1 billion a year in sales - and yes that is as a programmer. The largest had $86 billion in managed assests when I left and I did OOP development for them that handle payment processing, collections and repossession. I can't be closed-minded about things because I am expected to work withn the confines of existing code.

This guy must have been dropped on his head as a baby because little else would explain that kind of radical mentality...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

or maybe he grew up (*snicker*) using an archaic language like maybe ASM or COBOL.. (not that they are bad languages.. I love ASM, but I only use it when necessary)

I did see that he is touting "table oriented programming" .. which looks a hell of a lot like relational databases and OOP..
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i think (woow, hereby the statement already lowers in value :p) he got upset by all those people that claim that oop is the answer to everything and he had already an answer to everything....

i first learned turbo-pascal (only the procedural part, not the oo part) and the next year i had to learn java. at first oop felt really unnatural... but at some point you experience that oop gives you the power to do things that were harder to express/do in a no-oop (if you are a zealot you should read this as noob) language.

it comes down to this: pick the right tool for the job. i know there are people that have been hammering for the last 20 years and are able to plug a screw in wall... but for other people a screw-driver seems more appropriate.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

timvw wrote:it comes down to this: pick the right tool for the job.
... and an open mind is one of the best tools one can have.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

feyd wrote:or maybe he grew up (*snicker*) using an archaic language like maybe ASM or COBOL.. (not that they are bad languages.. I love ASM, but I only use it when necessary)
I started with assembler and basic on the C64 then moved to PC and Turbo Pascal and C. Then I had to learn COBOL and I can seriously tell you that I hate it by heart. Those restrictions are kind of self inflicted pain if you program. Thats why I like to just code without any restriction but on bigger programs OOP is a must.
Black Unicorn
Forum Commoner
Posts: 48
Joined: Mon Jun 16, 2003 9:19 am
Location: United Kingdom

OOP

Post by Black Unicorn »

After a long time of procedural programming, I gave OOP a go just because other people were doing it and I didn't want to be in trouble in case I had to deal with somebody else's OOP stuff. I figured that once I had some vague comprehension of the modelling language I'd return to procedural immediately.

However, I find it much easier to group code using OOP. I make shorter lines. I find that $target = $_SESSION["yay"]->Query("SELECT `eyeball` FROM `human`") is better than while ($target = mysql_fetch_array(mysql_query("SELECT `eyeball` FROM `human`"))). The only drawback I find is that debugging is slightly less straightforward, but this motivates me to write better code.

In short, I agree with the opinions presented that OOP is a matter of taste and personality, but also a must to understand and try out, especially if you are as disorganised as me. I used to have .inc files all over the place with this and that function which in the end would turn a day of programming into a week of housekeeping. Also I find you have a parameter to spare with OOP which means I can use one "instruction" rather than 2, such as

Code: Select all

$mm = $ball->radius("metric")
rather than

Code: Select all

$mm = getradius($ball);$mm = metric($mm);
Once I get better at OOP I'll post an even better example.
H
Post Reply