maxudaskin Posted July 3, 2008 Share Posted July 3, 2008 What is the point of a class? How do you use it? What are it's proficiencies? What are it's main drawbacks? Quote Link to comment Share on other sites More sharing options...
teynon Posted July 3, 2008 Share Posted July 3, 2008 PHP.net has an awesome user manual. k thanks. Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 Classes are great for when you want to make multiple objects. The idea is that you can run the same functions on different objects without changing any input parameters. Say you have a game website where a user it taking care a multiple pets. If each pet is its own object, when a user feeds pet one, all you have to do is pet1.feed() instead of feed(pet1). The need for this is very low in common web applications compared to many other types, but it is definately a luxury to have. It really depends what you are doing but I wouldn't say there are drawbacks, necesarily. Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 The point of a class is to: - encapsulate a set of data and the methods that will operate on it - enforce strict controls on data - support easy re-use and extension of existing code There are lots of ways to use objects, the main one is as follows, creating your own data type that is used by several objects, each of which is an instance of the class: class SillyString { private $str; function __construct ($value) { $this->str = $value; } function show () { echo '"', $this->str, "\"<br>\n"; } } In PHP 5 there are few drawbacks (there is a very slight perfomance loss, but that's no drama). PHP 4 had a terrible object model. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 3, 2008 Share Posted July 3, 2008 This a take home mid-term or something? The only reason I'm not locking / deleting it is you have a history of posts that look like genuine questions; so you're getting the benefit of the doubt. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted July 3, 2008 Share Posted July 3, 2008 actually i was wondering the same questions. its good to know Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 3, 2008 Author Share Posted July 3, 2008 No, it is not a mid-term. I am in summer. I am trying to figure out how to use classes. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 4, 2008 Share Posted July 4, 2008 It is easier to implement change. It also can present a more clear and organized way of thinking/programming from what I've seen (and done,limited). The question for 'us' that do not yet fully grasp it is, where can we learn and switch over from procedural to OOP. "Read this book" isn't always useful unfortunately. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 4, 2008 Share Posted July 4, 2008 You can only learn the benefits of OOP by writing OOP and writing lots of it. It also helps to work in steps. 1) Stop writing procedural code. Just write everything as classes even if it seems like more work with no benefit. This will get you familiar with the syntax and "structural" components of OOP. Write a small project in this way. It doesn't have to be a web site. It can be a command line script that runs a simulation or anything else. But write something. 2) Read about inheritance. Think about how you could have used inheritance in your project from step #1 to decrease the amount of work you performed. Rewrite the project from step #1 to take advantage of this. 3) Introduce a new object or component into your project. For example if your program calculates miles per gallon for cars, introduce buses, planes, boats, etc. Write them as separate classes. 4) Read about polymorphism. Think about how you could have applied that in your project to decrease the amount of work and rewrite your project again. 5) Keep writing OOP code. Start using it everywhere. Eventually you will start to see some re-emerging problems and road-blocks. At this point pick up a book on design patterns and the light bulbs should start really firing off. Quote Link to comment 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.