Gazanfer Posted October 4, 2007 Share Posted October 4, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/ Share on other sites More sharing options...
emehrkay Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-361765 Share on other sites More sharing options...
teng84 Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-362062 Share on other sites More sharing options...
roopurt18 Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-362071 Share on other sites More sharing options...
lessthanthree Posted October 8, 2007 Share Posted October 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-364831 Share on other sites More sharing options...
RichardRotterdam Posted October 13, 2007 Share Posted October 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-368524 Share on other sites More sharing options...
DyslexicDog Posted November 2, 2007 Share Posted November 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71818-need-advice-on-classes/#findComment-383856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.