Page 1 of 2

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

Posted: Thu Dec 29, 2005 1:54 pm
by Charles256
http://www.php-editors.com/articles/sim ... lasses.php

even my retarded self could follow along :-D

Posted: Thu Dec 29, 2005 2:21 pm
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...

Posted: Fri Dec 30, 2005 4:31 am
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.

Posted: Fri Dec 30, 2005 5:06 am
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

Posted: Fri Dec 30, 2005 7:26 am
by Charles256
if you read the last para he said it was sposed to be the very basics ;)

Posted: Fri Dec 30, 2005 7:36 am
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 .....

Posted: Fri Dec 30, 2005 8:28 am
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.

Posted: Fri Dec 30, 2005 8:40 am
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

Posted: Fri Dec 30, 2005 9:26 am
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.

Posted: Fri Dec 30, 2005 9:35 am
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?

Posted: Fri Dec 30, 2005 9:36 am
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.

Posted: Fri Dec 30, 2005 10:11 am
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.

Posted: Fri Dec 30, 2005 10:35 am
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?

Posted: Fri Dec 30, 2005 10:38 am
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.

Posted: Fri Dec 30, 2005 11:14 am
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.