kratsg Posted June 19, 2010 Share Posted June 19, 2010 So, I'm a relatively experienced programmer - I've never really had the option or chance to ever create a sort of application that would be best developed using classes and objects. I know the basics of OOP at the moment, but I do have a few questions. 1.) What tutorials have you found on the internet that seem to help give you ideas for when to use OOP or teach you more about it? 2.) You have a class which has multiple properties based on a single user-id. You instantiate the class with the user-id and it creates an object with some number of values. It seems annoying to have to re-instantiate the class with each page load to do anything with the object... or maybe I seem to be thinking about it wrong? 3.) What are the benefits of public functions/variables versus private functions/variables? 4.) What are the benefits of using a class to store multiple related functions rather than having a single file with those same functions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/205238-object-oriented-programming-oop-just-a-couple-of-possibly-stupid-questions/ Share on other sites More sharing options...
Alex Posted June 19, 2010 Share Posted June 19, 2010 1.) What tutorials have you found on the internet that seem to help give you ideas for when to use OOP or teach you more about it? I can't think of any particular online OOP tutorials offhand that were specifically useful. One book that I have, and would highly recommend, is PHP Objects, Patterns and Practice. 2.) You have a class which has multiple properties based on a single user-id. You instantiate the class with the user-id and it creates an object with some number of values. It seems annoying to have to re-instantiate the class with each page load to do anything with the object... or maybe I seem to be thinking about it wrong? Using OOP techniques you should be making your life easier, not harder, so if you find that using OOP is making things harder on you then you're not using it correctly. 3.) What are the benefits of public functions/variables versus private functions/variables? Different accessibility keywords allow you to maintain encapsulation. Briefly this means that you should not have to know how an object operates internally to be able to take advantage of its functions. You might compare this to a car; you know that if you perform a certain operation, like changing gears, the gear is changed. You don't have to know exactly how that works internally, and you shouldn't have to. Similarly if you have an object to perform a certain task you shouldn't have to know exactly how it operates on the inside, all you need to know is that if you call some method X you will get an expected and predicable result. These accessibility keywords allow you to ensure that properties and methods that shouldn't be touched from the outside aren't. 4.) What are the benefits of using a class to store multiple related functions rather than having a single file with those same functions? If you're just storing a bunch of arbitrary static functions in a class usually there isn't much of a benefit. You might see this done sometimes in like a Utility class or something similar where the only benefit is, as you mentioned, to group similar functions together. Quote Link to comment https://forums.phpfreaks.com/topic/205238-object-oriented-programming-oop-just-a-couple-of-possibly-stupid-questions/#findComment-1074298 Share on other sites More sharing options...
trq Posted June 19, 2010 Share Posted June 19, 2010 1) Ive not really found too many but have read several books including 'PHP 5 Objects, Patterns and Practice'. I use OOP for pretty much every project these days. 2) I'm not exactly sure what your getting at but it would seem to me a User object might be best tied to php sessions. 3) You should only expose enough of a classes API to make it useful. The inner working should not be known / accessible to the outside. 4) None. If that is all you are doing. Generally however classes are designed to work make functions with a common set of data. Functions alone do not relate to each other in the same way. Quote Link to comment https://forums.phpfreaks.com/topic/205238-object-oriented-programming-oop-just-a-couple-of-possibly-stupid-questions/#findComment-1074300 Share on other sites More sharing options...
kratsg Posted June 19, 2010 Author Share Posted June 19, 2010 This sounds pretty interesting. 1.) I'll get the book - I like reading all things PHP. This book: http://www.amazon.com/PHP-Objects-Patterns-Practice-Second/dp/1590599098/ref=sr_1_1?ie=UTF8&s=books&qid=1276972218&sr=1-1 . right? 2.) So, I should be passing the whole object into a session and just call the session as if it was an object instead? $_SESSION['user']->getName(); Wouldn't that seem like a lot of data stored inside the object being passed through the session slow down the page processing times? 3.) So, you're basically saying to make everything inside a class... 'private' and then start making things 'public' based on what you need to make it accessible enough to work? 4.) Exactly what would you mean by a static function? Functions with no arguments? I can't imagine any difference there... Does variable scope change within a class than within a file? IE: you can reference all the classes' variables within any function? Quote Link to comment https://forums.phpfreaks.com/topic/205238-object-oriented-programming-oop-just-a-couple-of-possibly-stupid-questions/#findComment-1074444 Share on other sites More sharing options...
Alex Posted June 19, 2010 Share Posted June 19, 2010 Yeah, that's the correct book. 3.) So, you're basically saying to make everything inside a class... 'private' and then start making things 'public' based on what you need to make it accessible enough to work? Not exactly. It should be as simple as possible. Say you write an image manipulation class. After you're done to be able to utilize all the functionality you shouldn't have to mess with internal variables, etc. There should be a straightforward API that allows you to do what you need without knowing what's going on inside. All of the logic should be self-contained in the object so you don't have to worry about it when using the object. 4.) Exactly what would you mean by a static function? Functions with no arguments? I can't imagine any difference there... Does variable scope change within a class than within a file? I mean like this: class Utility { static public function FileSize(...) { // ... } static public function TimeSince(...) { // ... } } Where maybe FileSize will take an integer and convert it to a nice format like KB/MB/GB, etc.. And TimeSince may take a timestamp and tell you in a nice fashion how long ago that was (eg. '54 minutes ago'). By declaring them static you can access them like this: Utility::FileSize(...); Utility::TimeSince(...); Without going too much into detail (you can look this up on your own) static methods and properties of a class don't require you to have an instance of the class to use them. You can read more about the static keyword here. Again, I'm not saying that this is particularly useful and offers any real advantage because grouping functions like this is not necessarily better than grouping them by the file in which they are stored. Just saying that you may see this sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/205238-object-oriented-programming-oop-just-a-couple-of-possibly-stupid-questions/#findComment-1074452 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.