I am a developer in PHP technology, I am aware of almost all the basics of OOPS, but still cannot find out the way to apply these concepts over a procedural programming.

I do it in very orthodox way, but I don't know why I am coding it in that way. I never have reasoning to justify my OOPS applications, which has inferred me that I am worst in OOPS, Please help me guys to understand the application of OOPS.

closed as too broad by durron597, user40980, user22815, gnat, Ixrec Apr 24 '15 at 23:23

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Hey OM, no need to get down on yourself. Learning takes time. One friendly piece of advice: it's not good practice to post questions with all caps. It really turns people off. – bitops Jul 25 '12 at 6:24
  • Ohh I didnt meant to make my words offensive... thanks for genuine advice.. But I am suffering from continous defeated in my skills when it comes to responsibilty, everytime the better one snatches away the project from me.. its getting hard to stay calm...I hope you understand – OM The Eternity Jul 25 '12 at 6:27
  • 2
    Invest your time to code a hobby application (like the stripped one that you do in PHP) in Smalltalk. It is the father of OOP, it has yet lot of ideas that are novel after being here for 30 years, and there is school of thought that all "new", "revolutionary" steps (C->C++, C++->Java, Java->Python||Ruby) were just stealing things already known in Smalltalk (OO, GC, dynamic typing). From my personal experience, nothing has given me more as for knowing OOP than a course of Smalltalk at college. – herby Jul 25 '12 at 6:32
  • 1
    You may try download Squeak and learn step-by-step using free Squeak-by-example book. It may be very helpful to get Kent Beck's Test-Driven Development By Example and try to follow as well. TDD is very useful methodology, it aims at simple and working design first, and is also very friendly in that you can take as little steps as you need. Lot of big minds in the area of doing OOP (and agile) right collaborate in www.c2.com wiki, where you can learn just by browsing, but that's a bit higher level. Of course, after some time, you should read the classic Design Patterns. – herby Jul 25 '12 at 6:46
  • 1
    BTW. this is a good article: surfscranton.com/architecture/KnightsPrinciples.htm A little funny way to show OOP from other than dogmatic way, and very true. – herby Jul 25 '12 at 6:56

Firstly, it's pretty difficult to summarise something as complicated as OO in a few sentences. I would recommend that you find yourself some good books on the topic, and read them, while practicing what they preach.

That said, a short explanation would be: object orientation is simply one particular way to structure code. You are trying to create pieces of code which are loosely coupled and cohesive. Loosely coupled meaning that they don't unnecessarily rely on each other, and cohesive meaning that things that should be close together, are. Encapsulation is the principle of making a class appear as simple as possible to the outside world, while containing whatever complexity is necessary, inside itself. You are also trying to reduce repetition and duplication of code and logic (DRY, etc).

These are the general principles, and you should spend a lot of time trying to understand why these are good principles, and what they mean. They tend to be fairly universal, so you will be able to apply it outside of OO. Once you have these "basics" (and they are not simple) down, you will understand the whys, and can make your own judgement calls on design. Unfortunately, I can't think of any way to go into any more detail without writing pages and pages, so I'll leave it at that. Last tips: try to make sure you understand why you are doing something; but be patient - I've almost never seen anyone who was more than barely competent in OO with less than 5 years of experience.

  • So my basics are not yet clear the way it should be to think over the designs... – OM The Eternity Jul 25 '12 at 6:43
  • 1
    @OMTheEternity Yes, simply applying dogma doesn't really help. A much more worthwhile approach is to deeply understand the reasoning behind it. Once you have that, the rest will come easily and naturally. It's a fairly long road there, however. – Daniel B Jul 25 '12 at 6:45

Its really very basic. Its about binding together the data and the logic that manipulates that data.

So you have:-

  • Encapsulation. all logic and data definitions related to the object are encapsulated in a class.
  • Interfaces. some data and methods need to be exposed to the rest of the world if the class is to be useful. Data and Methods so exposed form the interface.
  • Inheritance. You can make a more specialized class by inheriting the features and methods of a base class so you don't need to code the logic again.

All the other stuff Polymorphism, Abstract Classes, Sub Classes, Super Classes is pretty much icing on the cake and varies greatly form language to language.

  • Wrong. OOP is mainly about polymorphism. Encapsulation is useful concept, yes, and it is fine to have to public/protected boundary; and inheritance is frowned upon at least the last decade, saying "composition rather than inheritance". – herby Jul 25 '12 at 6:49
  • @herby -- true, but the implementation details vary so much from language to language its difficult to describe in a simple definitive way. – James Anderson Jul 25 '12 at 6:53
  • BTW, encapsulation is unhappy term, since it defined as (wikipedia) "In a programming language, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof: A language mechanism for restricting access to some of the object's components. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data." You mentioned the second definition, while I was always using the first meaning of the word. – herby Jul 25 '12 at 6:54
  • @herby -- both meanings are true for most OO languages. You bundle the methods and data together in a class and that class also restricts access its data and functions. – James Anderson Jul 25 '12 at 6:59
  • 1
    I know, of course, just ... sometimes the term is explained as one of it, sometimes as the second, sometimes as combination of both... and now, learner, be confused. – herby Jul 25 '12 at 7:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.