Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1566 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP Objects

Thread title: PHP Objects
Closed Thread    
    Thread tools Search this thread Display Modes  
07-05-2005, 05:44 PM
#1
Travis is offline Travis
Status: Member
Join date: Jul 2004
Location:
Expertise:
Software:
 
Posts: 445
iTrader: 0 / 0%
 

Travis is on a distinguished road

  Old  PHP Objects

I was looking at the Classes and Objects section of the PHP manual as I now understand these concepts alot better but was wondering when others put objects in PHP to use?

Since alot of PHP work out there uses users input from forms and such when would you use objects. Obviously there is no point in creating an object when your objective is to so something with some user input.

An object has a state and a behaviour, once a page has been fully interperoated all of this is lost. As a result I can't see the use of objects to be really used in the same scenario as say Java. For example in Java an object may represent a real life thing. Say you have an enemy in a computer game you have 5 of them so you create one class and 5 objects which can all have different states and be doing different things. I can't really see how you would put this to work in PHP.

In addition since you have to load a new page to receive input from a user the state of an object is lost and even if you have an object you lose the previous data. I think that is why databases are relied on very heavily with php because it is your best means of storing any of that information (in addtion to what they would be used for in any program any way).

Thought/Comments on this please?

07-05-2005, 06:15 PM
#2
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

I don't use classes or functions myself. I want too, but I don't see why I'd need them since I mostly work with queries and if/else statements.

07-05-2005, 06:53 PM
#3
Koobi is offline Koobi
Koobi's Avatar
Status: Member
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 312
iTrader: 0 / 0%
 

Koobi is on a distinguished road

  Old

NOTE: i will refer to functions within a class as member methods or simply methods and the usual functions will be referred to as functions to avoid any confusion.

inheritance in OOP probably saves me hundreds of hours.

i have base classes that have extends which are what make my code so much more easier to read.

for example, my database class has the basic methods, like a generic querying method and the other usual db related stuff, i extend that class to make the queries specific depending on my project.

so i write the base class once, modify it maybe every few months if there's a bug in it and i only have to write the code specific to each new project. so thats a LOT of time saved. my god i would NEVER go back to NOT using OOP.

but the problem is, PHP is not truly OOP. it lacks a few OOP features too. OOP is also slower in PHP but it makes code so much cleaner and upgrades/patches/updates VERY easy.

since the HTTP protocol is stateless, for each new page request, the data is lost like Travis said, but if you wish to, you can transmit this data accross pages via sessions (also see: Object Serialization) but for my sort of work i hardly need to pass object data accross pages and i mostly work in creating CMS's and other user management systems.


so why should you use classes when you can just use functions? they seem to be the same thing, only classes seem to be an exaggerated version right?
well, within classes, you have a LOT more control over variable scope. you can specify which variables should be global or local within that class whereas if you wanted a variable to be global accross functions, that global variable would be global accross the entire script. so you have a LOT more control over variable scope within OOP which makes for safer and very robust and powerful code.
basically, you can create your very own language with OOP in a sense.

but like every programming paradigm, bad coding practice is possible within OOP too. a lot of people attempt to manipulate object properties (the variables within a class) directly whereas it is better to use 'getters' and 'setters'...of course that's just one tiny example, there's a million other things but then that's why god gave us google



if you want, i can send my class files over to you and maybe talk you through them on IM or something so that you can see how easy it makes my life, Travis



:edit:
i should also mention that there's a time for everything...OOP is not always the best thing. for tiny scripts like, tiny form processing mail scripts, etc., using OOP is overkill. it's wise to know when to use OOP and when not to

07-05-2005, 08:21 PM
#4
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

objects get put to use in php when you do lots of repeative tasks, for reusability. i created a mysql class and all i have to do in the php execution (index.php for example) is write the sql command and pass it to the object method.

it's saved me tons of time because i don't have to debug hundreds of sql statements. if there's an eror, i know it's in the sql command and not the function. if i want to change it, i just update the class once andit's carried over eveywhere it's used.

i treat OOP like i treat my CSS integration. comparing php class/methods to CSS classes is how i grasped the basics of OOP and comparing it w/ my fellow dektop programers and program architec designers who frequently work with OOP in things like java and C++ agreed it's a fair way to explain it w/ php.

07-05-2005, 08:47 PM
#5
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

Koobi, I'm making this pretty large CMS, would you mind sharing some coding methods with me over MSN? bastenfeld@gmail.com

I really want to learn OOP, because right now my code is full of queries and if/else statements.

Thanks

07-06-2005, 12:46 AM
#6
painperdu is offline painperdu
Status: Junior Member
Join date: May 2005
Location: Louisiana, USA
Expertise:
Software:
 
Posts: 37
iTrader: 0 / 0%
 

painperdu is on a distinguished road

  Old

I recommend reading through http://forums.devnetwork.net/viewtopic.php?t=30746 for one of the best discussions on this topic ever!

My take on this is if you plan on writing a heavy duty CMS or something similarly large then OO makes a lot of sense with PHP. On another note, if you only need small website functionality then OO may be overkill. Personally, I keep a few generic functions handy that I use over and over.

I agree that OO means more in Java than in PHP, but for very good reasons. HTTP is stateless and PHP was built for and fits this very well. A few years ago they tried to sell Java for general web work but now that the hype has cleared we see that the nature of the web doesn't favor such a memory hog as required for heavy duty OOP.

07-06-2005, 11:56 AM
#7
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

I'm almost done reading through this article and it is very helpfull

07-06-2005, 06:40 PM
#8
Koobi is offline Koobi
Koobi's Avatar
Status: Member
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 312
iTrader: 0 / 0%
 

Koobi is on a distinguished road

  Old

Originally Posted by Bas
Koobi, I'm making this pretty large CMS, would you mind sharing some coding methods with me over MSN? bastenfeld@gmail.com

I really want to learn OOP, because right now my code is full of queries and if/else statements.

Thanks

sure thing but i won't be able to chat much today, got some things to finish up but tomrrow is fine. also keep in mind that i'm not too good with terms. i learnt all this OOP stuff around...8-10 years ago, forgot it for many years (since i wasn't doing any coding for a while) and then relearnt it by doing. so i can explain stuff but don't count on me to use terms you would see in an article

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed