even my retarded self could follow along
One of the best introductions to classes I have ever seen...
Moderator: General Moderators
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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:
Less code, and easier to read, IMHO.
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 . '">';
}-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
Extending classes is a big value add over functions..Grim... wrote:It still doesn't explain what I find most confusing about classes - why bother with them?
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";
}
}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.
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?
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?
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.
But that tutorial is a great read for beginners.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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.
Here's another reason I don't use classes unless forced to...
What the <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> does that mean?
Code: Select all
Call to a member function on a non-object in etc.etc.- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
class foobar
function foo() {
$this->_getBar = $this->bar();
}
}hehJcart 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.
Cool.