Page 1 of 2

Overwhelmed

Posted: Mon May 28, 2007 2:47 pm
by s.dot
These past few days I've been trying to learn and implement very basic principles of object oriented programming. I've looked up multiple articles about MVC, tried somewhat coherantly to write a php5 database wrapper class (simple you'd think?), and have been separating coding logic from presentation using template lite.

It honestly feels like I'm learning a new language all over again. Different way of thinking. Most of you on this forum who design with these practices learned it in another language. I'm learning it in PHP, has anyone else learned it in php?

I know I'm going in the right direction to coding higher level applications, learning good practices for other languages, and developing applications that other people can look at and understand what's going on. But, how long before it just "clicks" in my head? :?

Posted: Mon May 28, 2007 2:56 pm
by superdezign
I learned OOP in C++. But I didn't really understand it until I took a Java programming class in high school.

It's hard to explain, so everywhere you try to read will claim to try to explain it better than everyone else, but it'll all just sound like the same jibber-jabber, and eventually you scroll and skim quickly, and decide from that whether or not it's worth your time to read.


I learned it as objects being people with jobs, but most people have specialties. Each job title, so to speak, is a class. Each action that's part of their job is a function/method.
Oftentimes, people with different jobs have to communicate, and they need a middle-ground to do so. That's your application.

You define the people externally, and then you create the people and cause them to interact internally. And screw anything you hear about inheritance... I think it's horrible for them to discuss it so early on, but it's like they all do.

Re: Overwhelmed

Posted: Mon May 28, 2007 3:06 pm
by Christopher
scottayy wrote:But, how long before it just "clicks" in my head? :?
I wondered what that clicking sound was. ;) Seriously it will never just click completely (at least it hasn't for me). You just need to have a problem that you need to solve and implement it poorly, and then better, and then better still. You never get to the point where you get it perfect because the state-of-the-art keeps advancing. As you learn, certain problems become easier to visualize and solve. But that only opens you up to solving more difficult problems and you are back to learning again. ;)

Posted: Mon May 28, 2007 3:50 pm
by jayshields
I would say it's easier to learn OOP from languages such as Java where you are forced to do it. In PHP it's easy to skip stuff/cheat and get away with it.

Posted: Mon May 28, 2007 3:52 pm
by onion2k
jayshields wrote:I would say it's easier to learn OOP from languages such as Java where you are forced to do it. In PHP it's easy to skip stuff/cheat and get away with it.
I'd agree with that. Objects clicked for me when I was mucking about with Ruby.

Posted: Mon May 28, 2007 5:28 pm
by Maugrim_The_Reaper
The "clicking" always takes time. And it only clicks after making a few really stupid mistakes where you suddenly notice where a design shift would make things a lot easier. It's definitely discouraging at first - I hated OO when I first started learning it. It made no sense, couldn't clearly be seen, and it was a chore to get anything working. One day it just clicked and since then OO has been only way I could imagine programming.

I really think it's the basics that get people. A class must (it's not optional!) be specific and representative. That's cool on paper, but in practice? It's a nightmare when starting out. You start adding functions, then they're needed elsewhere, then you realise they're needed everywhere. Eventually you'll either give up or figure out that what you did wrong - overloading the class with too much responsibilities so that all the other selfish piddling classes want a piece of the action ;). And that's just one learning experience in OO - there's a lot more. It took me a while to meet all of them, and a few books before I really understood them, and even now I sometimes question my knowledge (and therefore keep reading books to learn more).

I feel your pain, brother. Stick with it though, it a struggle well worth winning in the end. You have a whole forum here to help out when you need us ;).

Posted: Mon May 28, 2007 7:49 pm
by Jenk
OO clicked with me in Java, but there is clicking .. and there is clicking - where you realise that objects are not just objects, but should be finely tuned objects.

Posted: Mon May 28, 2007 10:44 pm
by alex.barylski
OOP is a paradigm. So IMHO it'll never click as it's not a technical flaw which is either right or wrong. It'll take time.

Don't worry about getting everything right first time through, natural progression is cool.

OOP is also a state of mind and a conceptual approach to developing software it has nothing to do with syntax of Java, PHP, C++ or others.

In fact, you can seem OOP applied in a procedural C by looking into GTK, GNome or GLib (I can't remember) but the whole library is done in C using Object Oriented practices. So indeed you can apply OO techniques even in procedural languages. Hell I have an old Assembler book "Mastering Turbo Assembler" which implements OOP techniques.

Language has nothing to do with it, but some arguably "force" OO practices on you a little more than others - which IMHO is the wrong way to learn. Much like how PHP hides low-level memory management from us, langugaes like Java do the same for applying techniques, etc.

Once the concepts of "why, where and when" you would use private, public and protected are understood, then a language which checks those things for you is no dought an assistance, but while learning OOP I would argue it's best to start fropm ground zero - but thats maybe just me, I can't accept something for the truth until I understand it's history, it's natural evolution and current state and how it plays it's part in what it is I am going to be using it for.

My advice would be: Don't bother focusing on MVC and design patterns while obsorbing OOP basics, otherwise, you will be overloaded/overwhelmed. If you are now comfortable with PHP procedurally, begin by converting some old projects into more OOP friendly.

Start by taking a simple, well known procedural API in PHP and writing a OO wrapper. Databases are difficult because they strive to acheive abstractions for multiple libraries, thus making it an abstraction layer rather than just a high level wrapper.

For instance, if you are familiar with a certain functional API such as the String API:

http://ca3.php.net/manual/en/ref.strings.php

Start by wrapping the functions into an class called Scottayy_String using just a simple API modelled after well known String classes.

http://msdn2.microsoft.com/en-us/librar ... S.80).aspx

or if you prefer Java.

http://java.sun.com/j2se/1.4.2/docs/api ... tring.html

Either will provide an excellent template from which to get started on the right path.

Cheers :)

Posted: Tue May 29, 2007 4:56 am
by Kieran Huggins
I learned OOP with PHP, don't feel bad if it doesn't turn on like a light bulb... it'll take a while. It's more of a mindset than a rulebook anyway, stay the course!!!

Posted: Tue May 29, 2007 6:11 pm
by Ollie Saunders
scottayy wrote:I've looked up multiple articles about MVC, tried somewhat coherantly to write a php5 database wrapper class (simple you'd think?), and have been separating coding logic from presentation using template lite.
You tried the model-view-controller pattern and database API abstraction. Neither of those things are particularly easy for someone who has never tried to do them before even if that person is experienced with objects. I wouldn't be disheartened.
Most of you on this forum who design with these practices learned it in another language. I'm learning it in PHP, has anyone else learned it in php?
I learnted what objects and classes etc. were and applied them in C++ mostly because I was just fasinated by everything about C++, it's so damn cool (geek chic if you will). It wasn't until using PHP that I actually felt I needed them and had genuine purpose to use them and looked into applying them properly i.e. refactoring, design patterns and unit testing. Many of the features of C++'s OO model are actually frowned upon now and are known to be harmful.
But, how long before it just "clicks" in my head?
Two years for the average person, I've heard. But there are levels of knowledge.
supeerdezign wrote:And screw anything you hear about inheritance... I think it's horrible for them to discuss it so early on, but it's like they all do.
If you are referring to the tendency for people to overuse it, then I agree. But inheritance is an absolutely cruical part of OO. In fact I would go so far as to say OO wouldn't exist without it.
I would say it's easier to learn OOP from languages such as Java where you are forced to do it. In PHP it's easy to skip stuff/cheat and get away with it.
Yes I agree. A PHP framework will do largely the same as long as the desire to practice OO is there.
Maugrim_The_Reaper wrote:You start adding functions, then they're needed elsewhere, then you realise they're needed everywhere. Eventually you'll either give up or figure out that what you did wrong - overloading the class with too much responsibilities
Like fighting rapids in a raft you'll feel like you are going forwards and backwards, forwards and backward. I pinch of self criticism and self belief will ensure the mean of all that bidirectional activity is the gradual meander round the twisting bends of the OO paradigm leading to green banks and tastey partsures.
Kieran wrote:it'll take a while. It's more of a mindset than a rulebook anyway, stay the course!!!
Yes, but you need the rulebook, possibly many, at first. It's like anything, you learn the rules first, and then you learn where you can break them.
<stuff hockey said here>
There is nothing like having the rules and conventions of the languge itself enforce certain behaviours on you. That's why I love PHP 5's E_STRICT errors you get when you subclass poorly. The people who write OO code in C etc. are people who are already familiar with OO and no doubt the limits of C as well.

Posted: Tue May 29, 2007 8:00 pm
by superdezign
ole wrote:
supeerdezign wrote:And screw anything you hear about inheritance... I think it's horrible for them to discuss it so early on, but it's like they all do.
If you are referring to the tendency for people to overuse it, then I agree. But inheritance is an absolutely cruical part of OO. In fact I would go so far as to say OO wouldn't exist without it.
What if I were to say that I've NEVER used inheritance (in PHP)? That, if I have an object with multiple types of itself, I'll usually put like, a flag in it of some sort labeled "type."

However, I've never really run into a point where I've needed it in web development (games is another thing). Is there something that I'm missing?

Posted: Tue May 29, 2007 8:29 pm
by Christopher
superdezign wrote:Is there something that I'm missing?
Uhhhhhh .... the benefits of inheritance?

Posted: Tue May 29, 2007 9:40 pm
by alex.barylski
superdezign wrote:
ole wrote:
supeerdezign wrote:And screw anything you hear about inheritance... I think it's horrible for them to discuss it so early on, but it's like they all do.
If you are referring to the tendency for people to overuse it, then I agree. But inheritance is an absolutely cruical part of OO. In fact I would go so far as to say OO wouldn't exist without it.
What if I were to say that I've NEVER used inheritance (in PHP)? That, if I have an object with multiple types of itself, I'll usually put like, a flag in it of some sort labeled "type."

However, I've never really run into a point where I've needed it in web development (games is another thing). Is there something that I'm missing?
eh?

I'm trying to understand what your doing here, doesn't sound OOP. Instead of inheritence you add conditional logic in your object?

Drawing on the bird analogy:

You have three classes:

a) Seagull
b) Duck
c) Chicken

How would you those three objects without using a base class of type "bird"?

Using inheritence allows each of those objects to share common attributes and functionality: most birds (that I know of) have wings so that is a attribute which they would all share. However not all birds fly, for instance the chicken. So obviously flying isn't an method or characteristic you would want to be in the base class.

Do you use a single master class to emulate each?

Code: Select all

Class Bird{
  function __construct($type)
  {
    switch($type){
      case SEAGULL:
      case DUCK:
      case CHICKEN:
    }
  }
}
Cheers :)

Posted: Wed May 30, 2007 1:51 am
by Arawn
But, how long before it just "clicks" in my head?
How about ten years? Teach Yourself Programming in Ten Years by Peter Norvig

I sometimes think with OOP, ten years might not even scratch the surface.

Posted: Wed May 30, 2007 3:10 am
by Maugrim_The_Reaper
It clicks depending on how much you're practicing. More practice = faster click. Once it has clicked you have the OO basics - but the next step is learning how to use it wisely. The two revolutionary topics thereafter are Design Patterns (some of which are simple enough to go read now like Strategy, Singleton, Registry and Composite since they demonstrate a lot of why OO is cool ;)). The second is Unit Testing which forces you to test your OO code and keep it working in the long term with fewer bugs.

IMO, the Strategy Pattern seems to help a lot of people with OO. It's a simple pattern which demonstrates a few OO principles like inheritance and composition. Try writing a set of classes which support the use of bcmath, gmp, or big_int (in PECL) depending on which is available at any one time. It's a small exercise that could teach you some simple principles. It nearly always follow the Strategy Pattern too...