One of the best introductions to classes I have ever seen...

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

One of the best introductions to classes I have ever seen...

Post by Charles256 »

http://www.php-editors.com/articles/sim ... lasses.php

even my retarded self could follow along :-D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yep... that's a pretty good intro I'd say. There's really not a lot to it once you grasp the basics...
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

That shows true understanding and the ability to share knowledge. I hate it when people are all snobby and over-the-top with all their shnazzy jargon and buzzwords and don't actually get down to the nitty gritty. Like most of my college professors actually, they are right up there on their ivory tower. But anyways... good to see some people trying to make it simple.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

a bit too dumbed down for my liking, theres plenty of other things that it could of explained such as public / private etc,

hopefully they will add more to this tutorial in the future
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

if you read the last para he said it was sposed to be the very basics ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah that's just the core principle of OOP. Obviously they could have rambled on about public/private/protected (those are PHP5 only) inheritance, overloading, interfaces etc.... but then the whole thing has not acheived what it was supposed to do :D

KISS -- Keep It Simple .....
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

It still doesn't explain what I find most confusing about classes - why bother with them?
Taking his Page class as an example, why not just use functions:

Code: Select all

function Display($Title, $Keywords, $Content) {
	echo "<HTML>\n<HEAD>\n";
	DisplayTitle($Title);
	DisplayKeywords($Keywords);
	echo "\n</HEAD>\n<BODY>\n";
	echo $Content;
	echo "\n</BODY>\n</HTML>\n";
}

function DisplayTitle( ) {
	echo "<TITLE>" . $Title . "</TITLE>\n";
}

function DisplayKeywords( ) {
	echo '<META NAME="keywords" CONTENT="' . $Keywords . '">';
}
Less code, and easier to read, IMHO.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

well,here's a silly arguement... that code minimizing feature..you can minimize a whole class then copy and paste it elsewhere, with functinos you'd have to shrink x amount of functions then move em :-D hehe, I win. :-D
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Grim... wrote:It still doesn't explain what I find most confusing about classes - why bother with them?
Extending classes is a big value add over functions..

Code: Select all

// Original class
class Page {
   var $Title;
   var $Keywords;
   var $Content;

   function Display( ) {
     echo "<HTML>\n<HEAD>\n";
     $this->DisplayTitle( );
     $this->DisplayKeywords( );
     echo "\n</HEAD>\n<BODY>\n";
     echo $this->Content;
     echo "\n</BODY>\n</HTML>\n";
   }

   function DisplayTitle( ) {
     echo "<TITLE>" . $this->Title . "</TITLE>\n";
   }

   function DisplayKeywords( ) {
     echo '<META NAME="keywords" CONTENT="' . $this->Keywords . '">';
   }

   function SetContent( $Data ) {
     $this->Content = $Data;
   }
 }

// Now I want to extend display a bit..
class fancy_Page extends Page
{
    function DisplayTitle() {
    echo "<title><b><font color='red'>" . $this->Title . "</font></b></title>\n";
    }
}
Now fancy_Page has a different title function (yay fancy pants), but inherits all the other variables, functions, and so on.

Imagine if the Page class was 1,000 lines long, and you wanted to change *one* function in it. Thats where Classes become incredibly powerful - reuse, extension, and inheritance.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Grim he explained on the first page, with the door metaphor.

Let's say you have the function turn_key and another function called get_lock_position, in a class the "lock position" variable is only global to that instance of the class, so you can multiple doors, each having their own position, you could have a 4,000 element array each element of which contains a door. This is just one benefitc, can you do that with functions without it getting really dirty?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

It's good, but it's definately not as good as some of the ones I've read in books. Those go more in-depth, so you have a more solid foundation for learning. The less question marks an article leaves you with, the better. And also, the more expanding on mentioned concepts and practices that it does, the better. Just my opinion. :)

But that tutorial is a great read for beginners.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I was driving along the other day and I thought of a good metaphor of OOP..

OOP is a human, a human is compiled of miniature cells with extremely specific functions/tasks; so is a class; ;)
Classes should essentially have very specific purposes, and if you find them doing anything beyond that scope, most likely it belongs in another class. Don't beat yourself if you don't get it right the first or second time. When I write classes I find myself rewritting it several times until I am happy with the methods, and flow.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Here's another reason I don't use classes unless forced to...

Code: Select all

Call to a member function on a non-object in etc.etc.
What the <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> does that mean?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

class foobar 
  function foo() {
     $this->_getBar = $this->bar();
  }
}
The method bar() does not exist, so it will give you that error.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Jcart wrote:I was driving along the other day and I thought of a good metaphor of OOP..

OOP is a human, a human is compiled of miniature cells with extremely specific functions/tasks; so is a class; ;)
Classes should essentially have very specific purposes, and if you find them doing anything beyond that scope, most likely it belongs in another class. Don't beat yourself if you don't get it right the first or second time. When I write classes I find myself rewritting it several times until I am happy with the methods, and flow.
heh

Cool.
Post Reply