PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old December 21st, 2003, 04:59 PM
mwichmann4 mwichmann4 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 80 mwichmann4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 45 sec
Reputation Power: 5
Php Obj Help Ahhhh

Hey everyone,

I understand the basics of Classes and Objs with php and stuff, but my problem is understanding how this can help me in my programming. I need real life examples maybe just a small demo thing i could try and code. I know OOP is good but i just can't figure out how to put it to use. If you could assign me little projects for me to do that would help me see how much more powerful OO vs non OO please help. I hope you understand where i am coming from. I understand the syntax and all i just need ideas and how i can use it and understand it better in real life. Thanks
__________________
Nothing is Everything

Reply With Quote
  #2  
Old December 22nd, 2003, 08:05 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I find objects most useful for things I use over and over again. For example, if you need to secure several Web applications and don't want to rewrite code over and over or have multiple versions of it, write a security class that can easily be instantiated. Or consider writing a database abstraction class that you can use across your codebase. Here's a real life example. I have cause to use at least three different types of database connection and the related functions. As you may know, PHP's mysql, odbc, and sybase functions differ slightly in syntax (parameter order, etc.). Rembering the differences or having to look the differences up each time I need to hit a database is a pain. So I wrote a class that does the remembering for me. I instantiate my object and tell it what kind of database I'm connecting to. Then, for any database, I call the same methods and the class calls the appropriate PHP functions. In all cases, $db->fetch($sql) returns an array of row arrays; and $db->query($sql) executes a query that I expect no return from.

There's no point in writing classes for the sake of writing classes, but if they can help you to better organize your code or to make portability easier, then you should consider using a class.

Reply With Quote
  #3  
Old December 22nd, 2003, 02:08 PM
mwichmann4 mwichmann4 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 80 mwichmann4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 45 sec
Reputation Power: 5
ok i understand a bit better, thanks so much.

Reply With Quote
  #4  
Old December 23rd, 2003, 08:10 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Another thing objects are good for is if you know that you'll need multiple instances of something. For example, in some cases in my code, I have to connect to more than one type of database in the same script. I can create multiple connections to the different databases using my database layer, and I can manipulate them transparently despite their differences using the same functions.

For another example, if you were to write some sort of game, you might write an object to represent a card or a ball or some other item on the screen. The class defines the values and actions associated with the object, and you can instantiate it many times to create multiple objects. Further, if you need to change the fundamental behavior or values associated with an object type/class, you can do it in the one place.

Classes are also nifty because they're extensible. That is, you can write a parent class that defines behaviors and attributes that child classes have in common. Then you can write child classes to extend that functionality in more specialized ways. For example, I wrote an output class that takes database results and spits them out in different formats (XML, PDF, XLS, HTML). Obviously, each format requires specialized output functionality, but they also have certain things in common, such as methods for setting the data. So I wrote a parent class to contain common methods and values and then extended the class for each of the subtypes of output, eliminating duplicate code.

Here's another real life example. A few months ago, I found myself writing a lot of PHP code that simply printed out HTML forms. What I was doing wasn't large scale enough to really merit using a template engine, but I was sure tired of having to print out table rows and form elements. So I wrote a form class with basic methods add_field() and execute(). You instantiate the form, passing it a name, method, and some other optional parameters (whether or not to turn on js validation, for example). Then you add fields, passing the row, column, field type, name, value, and other optional parameters. You can also add labels, blanks, etc. The class supports all field types, but the method for adding them to the queue is the same, with different fields needing different of the optional parameters. When you use execute(), the form is built in a table with no tedious work on my part. I can build a fairly complex form now in just a few minutes, keeping ugly HTML code out of my PHP without having to run everything through a template engine.

Don't know why I'm so prolific on this topic. I'll quit with that.

Reply With Quote
  #5  
Old December 23rd, 2003, 10:52 AM
mwichmann4 mwichmann4 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 80 mwichmann4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 45 sec
Reputation Power: 5
no that is totally cool, u are helping me soo much. I was wondering though, what kind of games could you make using php and mysql?

Reply With Quote
  #6  
Old December 23rd, 2003, 11:05 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Honestly, PHP (or the Web with the browser as interface) probably isn't the best medium for a game, but it's conceivable. Remember that OOP isn't limited to PHP. One example, though: I went on a Mahjong jag on my laptop recently. I was obsessed with playing the game, clearing the board faster and faster. Mahjong has some neat challenges, such as laying out a board at random with a pretty good distribution of tiles, some stacked, others not, etc. It's not a major problem, but as a little thought exercise, it's not bad. So to build my little Mahjong board, I figured I'd need a class to describe tiles, storing information about their face values, their coordinates in three dimensions, etc. And then I needed a larger organizing class, which I called Board, that generates and places the tiles based on settings such as maximum stack height, board width and height, and the available tile faces. In order to actually make the game work, I wrote some js, since passing all that info about tiles and their states back and forth across the Web was going to be inefficient. But the real lesson was in finding a way to organize the game so that the board would get a pretty good layout each time. So while the Web may not be the best medium for writing games, there's much to learn from trying to do little projects like this that help you practice fundamentals. Tic tac toe might be more manageable over the Web.

Reply With Quote
  #7  
Old January 26th, 2004, 04:02 AM
barraca barraca is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Spain
Posts: 4 barraca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi all, the functions that you have created are very usefull, becuase for example you can make an include file, and use it over and over in each new project, so probably I will do (after reading this), my own SQL call DB function, (it is a bit more of work now, but once you have it it is all much more easy).

A game that you can do in php is the "hundir la flota" (in spanish), in english would be something like Battle Ships??.

It is an excercise that they ask in college (for the C programers), so probably would be a very good excercise for you.

Take care.
Daniel
____________________________________________
Webmaster of: http://www.hotel-netguide.com

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Php Obj Help Ahhhh


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway