Jump to content

Need advice on classes


Gazanfer

Recommended Posts

I was coding only procedural until recently, and i was doing fine.

 

But now i'm working in a corporate environment, and i am expected to code oo. No matter how hard i try, my classes end up being next to useless.

 

I know the concept, i almost swallowed the php manual. I know the logic. What i lack is, i don't have the experience.

 

I simply don't know which classes should i construct. Let's say i'm building a regular user login register page. I can't decide if the user is an object (obviously it is), or if form elements should be objects, or if whole form is an object. (I don't need an answer to this question, just trying to make a point :))

 

I think i need some real application examples on which entities should be objects and how they should be constructed.

 

Any advices on how to progress?

Link to comment
Share on other sites

I think that the best thing for you to do would be to look at the work that is being done at your job. Look at how they are creating forms (similar to a login script), where they are processing the data, and how they are using procedural vs oo coding.

 

Look into design patterns as well. MVC (model view controller) seems to be the most popular approach when building larger oo applications. Once you get an understanding for how MVC is supposed to work, you'll begin to think about oo a little differently.

Link to comment
Share on other sites

hmm for me you can turn your functions into classes when they are used over and over again and most of your page use it but if your functions can only be use in say 1 or 2 page i guess better to stick with functions.

 

I some times make my fumctions as classes when my function relates to each other like

and they sometimes use the same variables!

 

so far database connectivity and querying, paging, dynamic html eg.. textbox,drop menu, mailer

are the most common function of classes that i use often.

Link to comment
Share on other sites

Use them where they make sense.

 

I use the class keyword in PHP4 to create a name space to wrap up common database access functions.  For example, the blunt of functions that return user data are accessible from UserDAO.  I don't actually instantiate a UserDAO object, I just call the methods statically, i.e. UserDAO::getUser($id).

 

Some people like to create objects to encapsulate the data returned from the database.  Personally I find this is rarely necessary.  In most cases, you are just querying for data for simple output; for this, arrays work just fine.  There is no reason to create 200 objects just to display a table with 200 rows of some type of data.

 

I frequently use objects to represent entities that are part of a site's framework.  For example, I might have a Page object that defines characteristics common to all pages.  Each page in the site is derived from this object and overrides functionality where necessary, but otherwise using the base functionality.  This is the type of stuff in web development that I use OOP the most frequently for.

Link to comment
Share on other sites

Seriously, a short while ago I too had this problem. I consider myself an advanced PHP programmer, but having only ever learned scripting languages, OO was somewhat a new concept to me so I had a bit of trouble getting to grips with it all.

 

Classes / Methods etc all made sense to me and I could use other peoples classes/libraries without a problem, but I always ran into an issue when creating things by myself. I didn't know which classes to construct and what each one should do.

 

I know alot of people don't use books to learn...preferring to 'learn by doing' and using examples already out there on the web, but the Apress book 'PHP 5 Objects, Patterns, Practice' is absolutely superb at filling in the gaps that most web tutorials leave out. It'll tell you how to construct classes, but also the important part of WHY  you should create a class to perform tasks. It really is the most useful book I've ever owned to do with PHP.

 

I would link to it, but I'm not sure if this board allows it, so you can find it at the usual retailers (cough, amazon)...Honestly, I can't reccomend it enough.

 

Link to comment
Share on other sites

The only way to learn OOP is by doing it at least that worked for me. At first you are likely to mess up a couple of times. But the more you do it the more your code will make sence. And don't worry about making messy code at first in the end it will be worth it. One other thing php!=Java so if you use a function over and over like a function that checks for sql injections or html in a string just use a normal function.

classes are great but don't overdo it

Link to comment
Share on other sites

  • 3 weeks later...

You said you went through the php manual and you understand the mechanics of OO. Maybe you should pick up a cookbook of some kind and work through the examples. I had a decent understanding of OO just didn't understand how to apply it like you. After I read the professional PHP5 book from wrox I'm much more proficient. The main thing to remember is planning the structure of an application is very important when building Object based code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.