iGuest 3 Report post Posted June 13, 2006 The concepts of OOP may seem daunting to many programmers and trying to make use of objects maybe trivial. This guide is to help you unravel your own potential so that you can start Thinking in Terms of Objects and to help those who are just starting out or are switching to an OO language. Notice from mastercomputers: I had to change my description of an object after being hit over the head by my missus. Women are not Objects, though it did make it more interesting to read and hopefully by what I say you can make your own judgement. Sorry if I offended anyone but I was sure this would make people Think in Terms of Objects better. Introduction In everyday life we are exposed to Objects yet we may not think of them as such (this was where I got assaulted and made alterations, leaving me to be more serious and respectful). For instance, the TV is an object which connects with other objects like the aerial connection to the back of it or the electrical power outlet. This connection is how these objects are interfacing with each other so that they can take advantage of what these objects can provide it with. What these objects are providing one another is their implementation. Take the aerial for instance, this object is connected to an antennae which receives broadcasting signals and directs it back to your TV, how it is doing this is the implementation that this object was developed to do and in most circumstances does not need a thorough understanding by the end user of how it does it as long as you get your reception on the TV. This object can also accept a connection from your VCR which shows how the aerial can be a reusable object and suitable for other object. The electrical power outlet is what you would want your own objects to be like, it is such a large ordeal that many objects of all shapes and sizes interface with this yet how many people really appreciate it. Off-topic: We currently had another large scale black out that wiped the entire power of our major city losing millions in revenue, this is when people started to appreciate it and now many questions are asked as to why this happened, who is to blame but more importantly, how can they resolve it so it will never happen again, since this is the second time it happened and we did not learn from our first mistake. On-topic: This shows how great your own objects can become and there are many developers who have created such objects that have benefitted the programming community, just remember you could develop that next big thing. Again however, it is not vitally important to the end user as to what drives the power to this electrical outlet, it could be a nuclear power plant, hydroelectric resservior, wind powered or even solar powered. This is just the implementation that is behind the scenes and this is how you can make your objects act like, where end users should not need to worry about what goes on in the background as long as in the foreground it does what they want. So What Makes an Object? This is quite a tough question and to be honest, I would answer with something obscure and riddling that would sound good yet cause more confusion. It is best if we dissect it and take out what is common amongst objects in a programming language and although I don't claim to properly answer this question, I do claim to shed some light on what an object could be and hopefully that light clears up some of the things that helps you make your own conclusion as to what a programming object could be. If we look at real world objects which I believe a lot of programming objects are based on, we can see some concepts that are similar for programming. In another thread which related similarly to objects and classes I talked about a dog as an object. For this I'm going to use the dog, but I'm going to introduce two new objects to clarify it even more, the owner and the trainer. Objects usually share two characteristics that should be for any object. This is properties and methods, terms that are related to programming so don't confuse our numerous definitions used in the English language. The property is basically instances that gives meaning and description a place. A dog, owner and trainer can each have a name property which is how we can refer to these objects individually, indirectly or even in a group. Sometimes you will call the dog by it's official name, other times you may decide to change it's name depending on the dogs behavior (which is another term for method). If DOG is BAD call it MUTT. The object is the dog that we are doing a comparison on, we are evaluating it's behavior or state that it is in or performing at that time (instance) and then using a method to change it's name property and then triggering an action or event that then makes us use our own behavior or method as a result of it. Now how was that for psuedo-code or shedding light on the subject? As you can see these things usually come naturally to us, which is how we would want to model our objects. The more natural you can make your object operate, the more likely you will be successful. So our dog consists of many properties, which can have many methods for how we can manipulate it. Some properties can not be altered which in our case are constants like the breed of the dog, other properties can vary like the name, color, state the dog is in, it's hunger, etc. Giving our dog a name and then providing it with natural methods that dogs may possess like barking, wagging tail, etc. Is just part of describing that particular dog, in a larger scale we can have an object that describes all dogs and what they are capable of doing, like for instance I'm referring to DOG which is a larger object that encapsulates those animals into that one group. This object is what makes it a good idea for interfacing all your dogs into, because they gain all the same functionality and it should be common, of course there are some things you must change in the object but you provide these methods so that those changes can be made without much hassle. There's some things in this object that classifies that the particular animal I am actually talking about is actually a dog and can fit perfectly in this class. I may have gone too far and out of basic scoping so I'm going to bring it back now. So we have a dog, an owner and a trainer. I was never good at telling jokes, so I'll leave that for someone else. So we have a dog, an owner and a trainer, the dog can interface with the trainer to learn new tricks. The trainer has methods to teach the dog new trick. Basically the trainer is extending the dogs capabilities and providing it with new methods or functionality that allows the dog to perform new tricks. This is showing objects interacting with each other, which is what objects should be capable of doing if the interface is correct. It's not meant to be possible to plug the aerial into the electrical power outlet, but it is possible none the less, though the electrical power outlet may not provide this object with what it provides the TV with it still has a method to handle it and that is what you must think about too, what other methods must your object provide to make sure that it's intended use is not abused, thi s should help you think about error handling and not missing the aerial, it too must have it's own way of handling errors too. The electrical power outlet decides it'll send a current down the aerial, the error supplies the current with a means to travel its wire and when it hits the atennae the atennae uses it's method to explode or catch on fire, or something drastic that it never knew it could do before. The owner as an object, sets some of the varying properties of this dog, by giving it a name, the owner has methods of handling this dog, though the owner may not know what the dog actually wants, the owner still has a means to work with the dog. So we have 3 separate objects, each having similar properties but different methods, when thinking about objects, it's a good idea to think about building blocks and separating your code so it does not become hard to maintain. Also helps that you do not need to have the trainer around all the time, because once the dog learns the new trick, the object of that dog should now reflect that it has learnt that method and can now perform it successfully without a trainer and the owner should now understand that the dog has learnt this new trick and has learnt a method of calling the dog and making it perform that trick. These are all happening at different times, but are happening, and the objects are updating themselves to reflect these changes. So back to the question, What is an Object? Well you can see that objects can be anything and everything, that is why explaining it in programming terms is quite hard, because literally everything in your program is an object, each character, each defined type, everything. Even properties are objects, even methods are objects, they each share characteristics of objects that suggest it's an object, so no matter what your missus says or methods she takes towards your actions, it's true. However chosing the right class to define the object is where all this error handling and assault usually arises from. Properties are more commonly the constants or variables within the container or other containers, Methods are more commonly the functions and actions that are triggered or called upon to manipulate our properties or properties of other objects. You Talked Too Much and I Still Can Not Think In Terms of Objects Look around you everything is an object, the toughest part is the implementation, what actually drives the object to perform its operation. I just pressed the switch on the TV to turn it on. I changed it's state property to being powered-off to being powered-on, the implementation that works with the action of me pressing that button is the coding you must think of performing for it, so I changed it's state, I must now call on another object to supply me power, another object to supply my reception, while inside my own contained object, I need to direct the electrical current to the correct components which means I have objects inside of my own object. If I were to explain every little detail about objects then I would be here forever, e.g. Now that the electrical power outlet is supplying the power, I must direct it to a fuse, the fuse being it's own object determines what to do with it and possibly passes it off to a capactitor (another object), which then performs it's methods and then sends it off maybe to a resistor, diode and so on. I know if I did explain every little detail, I would have written the ultimate guide, but time is a factor that controls my methods. That is why I'm leaving the coding aspects up to you, thinking about every object you require in your program is the daunting part about programming but must take place, but one thing I do think everyone needs when programming is to have fun with it. Try things out, make it perform badly, correct it, do everything you can possibly think of for your program, especially trying to get it to interact with incompatible objects and you're on your way to wasting time I know, but there's too many serious programmers out there who want to get the job done, and want it done quickly while the efficiency of their program and programming style reveals their true nature. I do not want to be known as a serious hardcore programmer with years of experience behind me and the ability to help many succeed in life, I just want to share knowledge and help others improve where I left off and in the same respect they can help me continue my quest for more knowledge. I could have based this purely on code but I find reflecting it against real and known objects easier to think of than saying I'm going to write a file handling class, the properties I want to have is the name of the file, the extension the file uses, the attributes I can set on this file, the owner and location, etc and for the methods I would like to have the ability to rename the property, change the attributes so that it makes the file read only, etc. These are the things you have to sit down, jot on a pad and really dig deep into what you really want to get out of the object you are going to develop. This message was proudly brought to you by MC with additional input from my missus. Cheers, MC Share this post Link to post Share on other sites
vujsa 0 Report post Posted June 22, 2006 Well, I've read this article twice now and the concept no longer eludes me but I still can't seem to work out how to implement this in code and exactly how it would benefit me in the end.Having said that, very nice article MC. I'm glad to see a nice easy to read explaination of what objects are. This has definitely made the concept much easier for me to consider. Right after I read this the first time I went and read several tutorials on the subject as it relates to PHP. I now understand for the most part the basic logic involved in writting and using PHP classes but still having trouble putting such code to use. I think my problem is that the simple examples are too basic to really show how this would work in a practical application. On the otherhand, I have trouble following the more complex examples that I have found. I recently flipped through a few PHP books that had OOP sections in them and found that PHP 5 has a much greater OOP potential than PHP 4. Anyhow, I've decided to buy a book to read up on the subject. I may even take a PHP class at one of the local universities. Thanks for motivating me to further research this subject mastercomputers. :(vujsa Share this post Link to post Share on other sites
minnieadkins 0 Report post Posted June 23, 2006 I'm still in college and I was exposed to a OOP environment in C++. I currently am helping devlop a piece of software in php that has a class or two in it. I find it hard to keep a OOP environment rather than just have a few classes to help me out.Once class in particular is a class to gather all variables sent via GET or POST and make them accessiable through an object. The class itself doesn't care what's sent to it, it just receives it. The variables are then accessible through the notation "$Object->PassedVariable". There are also several functions. One in particular is a print_debug function that prints all the variables passed to it (and session variables). One function prints hidden fields for variables or builds all the variables in the array as hidden form elements.In cases like that I can seriously see using a class, but I find it hard as well. Thanks for the interesting read mastercomputers. If I ever venture into Java I'm sure I'll understand OOP a little more clearly. Share this post Link to post Share on other sites
vizskywalker 0 Report post Posted June 25, 2006 I find that perhaps the most confusing thing about objects is the way various languages handle them. C variations does a fine job of treating objects as they should be handled, while I find Java does a decent job. I have not yet written any large scale scripts in PHP that could benefit from the use of objects, so I can't evaluate it. However, javascript objects are handled very poorly, in effect they are treated like special functions. Because of the various ways languages handle objects, after getting used to objects in one language, it is occasionally difficult to handle them in another language. However, if you start in a completely Object Oriented Language, such as C#, it can be easier to grasp the concepts, and once the concepts are grasped, it is easier to handle other languages' variations.~Viz Share this post Link to post Share on other sites
ruben1405241511 0 Report post Posted June 26, 2006 It was a nice analogy to read, because to me, it got a lot clearer. I'm mostly writing PHP right now, and (I think), that I've never used an object. I usually code exactly what I need and if I know I can use bits of code from another part of the page, I will copy-paste it from there.I've noticed that this style of working might be ineffective concerning my time (after all I don't remember all my code and I have to search through old pages, alter the code to fit in context, etc.), but I was never sure whether using objects would also make my code more effective in terms of performance.I hardly ever use (own) functions in PHP (except for repetitive tasks), because I write code like a book. I've come to notice that this is not the smartest thing to do, but I still find it hard to think "This function/object/method is a center of my programming, I will build it in this way, so I can apply it later". This seems to require a lot of experience.I'd like to manage that anyway. I am mind-mapping and writing down stuff like my database structure and technical ideas.Can you give some tips how to construct an object on the base on non-code ideas that are partly in my head, partly on paper, but not yet concrete in any way (for pages yet to write, for environments yet to invent etc.). Plus I'm mostly thinking in terms of usage right now. While this helps to make it usable, it is usually a long way to "terms of objects/code".@vujsa: What tutorials? Link, pretty please ;-) Share this post Link to post Share on other sites
vujsa 0 Report post Posted June 27, 2006 Keep in mind that I focus on PHP since it has become my language of choice. So any discussion on this subject is from that point of view. I think the best tutorial that I found that seems to have a moderate degree of funtionality and has a practical use is this one: http://forums.xisto.com/no_longer_exists/ This tutorial seems to give a general enough example that it could be easily adapted to other uses but specific enough to explain how everything works in a real world application. The following tutorial is a much more simplified tutorial: http://www.phpbuilder.com/columns/rod19990601.php3 This tutorial seems to have a practical use but I had a hard time following everything that was going on in the script. There are 6 pages in this tutorial which are difficult to navigate due to poor site design. I found the tutorials by using Google. The search terms used are PHP classes Tutorial and PHP OOP Tutorial. I'm working on a project right now so I don't actually have time to get into OOP. Once this project is done, I'll study the first tutorial I mentioned. It looks like the most likely to get me started on this new adventure. Hope This Helps. vujsa Share this post Link to post Share on other sites
ruben1405241511 0 Report post Posted June 27, 2006 Thanks a lot, I'll have a look at them. I'm in the planning phase of a new project right now and because I know I will modulate and add-on etc a lot later, I think it would be a good idea to get to know objects before I get going coding. Have a nice time! Php.net is always a good resource too of course. Share this post Link to post Share on other sites
Ananya 0 Report post Posted May 11, 2012 Creating objects for a particular class is a wonderful feature that all OOPs provide. By creating a class one creates the basic templete i.e assigning the properties or member variables and the functions all wraped up in a box.Now when we are creating objects of that class all the objects will share the memory space of the function the class provides.Only the member varibles for different objects will have unique memory space.i.e class A{int a;int b;void method(){}} Now when we create 2 objects for class A as A a1,a2;a1.a and a2.a will not share memory space as they are supposed to have unique values.But the memory space for method() is being shared by 2 objects a1 and a2.A very important feature of OOPs is encapsulation which is generally done by making variables as private and the function as public so that no one can access the variables directly like A.a or A.bAccess to these variables should be strictly through the method. Share this post Link to post Share on other sites