Page 1 of 2

I LOVE OOP

Posted: Mon Oct 31, 2005 9:55 pm
by dallasx
Here's the skinny... I changed majors from computer science because I could not pass the data structures class. Classes were my worst nightmare partially because we were programming in UNIX. Trying to make a graphical interface/morale booster, SOMETHING beyond a damn DOS prompt... I guess I just got depressed, who knows.

Now I love classes. Good God do they make life easier, for the most part, and the code structure so much cleaner. I just thought I'd tell everyone here that.

Posted: Mon Oct 31, 2005 9:58 pm
by feyd
welcome to OOP. :)

Posted: Mon Oct 31, 2005 9:59 pm
by dreamline
I totally agree m8... I switched to PHP OOP about 2 months ago and my sites definately got easier to maintain.. Problem before that i had to learn PHP first and after that i needed to clean up my code, so i definately love classes too nowadays... :D

Posted: Mon Oct 31, 2005 11:20 pm
by blacksnday
dreamline wrote:I totally agree m8... I switched to PHP OOP about 2 months ago and my sites definately got easier to maintain.. Problem before that i had to learn PHP first and after that i needed to clean up my code, so i definately love classes too nowadays... :D
haha... Im still in the staging of learning php code.

I badly want to switch my current code to OOP, and dread the day I finally
know enough to do it....
Whats makes it even worse. is that I have about 20 functions that almost do the same
thing.. Grab data and display it and just a tad
variation of how it is grabbed/displayed. I know OOP could reduce the code
and simplify it... however its the intitial learning that overwhelms. lol

At the rate my code is growing, and the more in depth it is becoming
*sigh* I wish I could easily grasp OOP....

Posted: Tue Nov 01, 2005 12:00 am
by dallasx
blacksnday wrote: haha... Im still in the staging of learning php code.

I badly want to switch my current code to OOP, and dread the day I finally
know enough to do it....

At the rate my code is growing, and the more in depth it is becoming
*sigh* I wish I could easily grasp OOP....
Honestly, the way I think it should be done (at least the way I got it) was to read a tutorial on how a simple class is structured (one class, one class variable and one method). Copy it and integrate a form so you can values(see below). Once you get it set up, load the page and start changing the value of the form field. Note how everything works and start experimenting with different ways to do stuff.

Try working with this till you get used to it.

FILE: names.php

Code: Select all

<?php
	include("name.class.php"); // INCLUDE THE CLASS FILE SO YOU CAN USE IT
	
	$set_name = new Name(); // INSTATIATE AN OBJECT TO BE ABLE TO ACCESS THE CLASS VARS AND FUNCTIONS OF THAT CLASS
	
	// ONE WAY TO DO IT
	$name = $_POST['name']; // GET THE FORM VALUE
	$set_name->setName($name); // PASS THE FORM VALUE TO THE FUNCTION setName IN THE Name Class to work with it
	
	// ANOTHER WAY TO DO IT
	$set_name->setName($_POST['name']); // SEND THE VALUE OF THE FORM DIRECTLY INTO THE CLASS
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="names.php">
  <p>Enter a name: <input type="text" name="name"></p>
  <p><input type="submit" name="Submit" value="Submit"></p>
</form>
</body>
</html>
FILE: name.class.php

Code: Select all

<?php
	class Name
	{
		// CLASS VARIABLES
		var $name
		
		// FUNCTIONS
		function setName($n) //CLASS FUNCTION (METHOD) TO ACCEPT A VARIABLE
		{
			$this->name = $n;
			echo "<hr>";
			echo "The name is set as: ".$this->name;
			echo "<hr>";
		}
	}
?>

Posted: Tue Nov 01, 2005 12:04 am
by dallasx
feyd wrote:welcome to OOP. :)
Thanks Feyd, it's like a sore pecker, you just can't beat it.

Posted: Tue Nov 01, 2005 12:09 am
by blacksnday
Hey, thanks for that example.
I will make it my project for tonight.

btw... perfect example for me to test with....
as my site currently relies heavy on forms
and for each form(about 5) they are seperatly coded. :oops:

Posted: Tue Nov 01, 2005 12:12 am
by blacksnday
haha... i do know something
var $name
is
var $name;
8)

Posted: Tue Nov 01, 2005 1:48 am
by howardr
i didnt start coding classes in oop till i leanred how powefull oop was in my computer science class when i leaned java. it definuilty does make maintaining sites easier. I can see where i am using redundent code on different pages.

Posted: Tue Nov 01, 2005 2:39 am
by blacksnday
After a couple hours of playing around i think i got it now!

Was able to convert one of my Form Submission code to
OOP and even figured out how to allow functions from my
other class
(the way I have been using it is far from classy)
to work inside of and together with the new class.

Thanks for the eye-opener :P

My project now over the next week is to convert
rest of my code to OOP then i can relax and smile a bit!

Posted: Tue Nov 01, 2005 2:55 am
by Jenk
I'm still coming to grasps with OOP in PHP, I've been a programmer for several years (unfotunately never in a position to start a project from scratch except in the little free time I have as a hobby).

However, I have seen some peoples examples of 'OO code' and quite simply, the only reason it can be called OO is because it has a class definition and that's it :|

It's the separation, or layering I love 8)

Posted: Tue Nov 01, 2005 4:43 am
by blacksnday
Guess im having trouble understanding what the use of the

Code: Select all

$this->foo = $bar;
constructer is for?

Inside the class $bar is already declared using var $bar;
and then its also defined... outside the class. So its being
defined(declared?) a tleast 4 different times.
I dont understand the need for so many.

I do understand that using var $bar;
means that var can be used by all functions in the class.

Then with the $this->
$bar will still work correctly if not using $this->

Someone teach me something real quick. lol :P

Is $this-> used for like.. if you want to have a function specific meaning
inside the class?

Posted: Tue Nov 01, 2005 7:34 am
by Maugrim_The_Reaper
It's a matter of scope, I think, if i read your post right...;)

Code: Select all

var $bar;
This declares a "class property"; a variable that may be altered at any time. Its specific to the class (and any extending child classes). Under PHP5, you could declare this private, public or protected (basically limiting where it can be accessed/changed). You use it outside in other code where the class is available as an instantiated object using $object->bar. $this->bar is used inside the class code; the "bar" property of "this" class...

Code: Select all

$this->foo = $bar;
Here we have two variables. $this->foo which is a class property (variable, but "class property" makes is simpler to "get" more cleanly what it is). $bar in this case is NOT a class property - it's a local variable within a class method/function - the same as any variable you've created in a function without OOP. See, has no leading $this-> saying it belongs to a the class.

Basically:

$this->bar != $bar; two different variables. One is a class property, the other a local function variable. In terms of scope - $bar can only be referred to inside its parent function (unless you use evil globals ;)). $this->bar (declared using "var $bar") is a class variable which can be accessed by any function or method within its parent class or extending child classes. I suppose you can liken it to a Global (in a limited sense) - you can use it in any class function, or refer to it like $class->property in other procedural code.

Posted: Tue Nov 01, 2005 10:24 am
by Moocat
OOP hit me like a truck one day.

I was calmy working away trying to understand the purpose and how OOP fit together when it just clicked into place. I'm still looking for a place to use it and will probably recode my game when I finish the playable "beta" mode (especially since I need to clear up a bunch of regular code too). I've been following Maugrim_The_Reaper's work since he seems to code well (IMHO) and in OOP format. I've been disecting his work and it's helped quite a bit (I just wish he'd turn on comments in his blog!).

Posted: Tue Nov 01, 2005 10:33 am
by dallasx
blacksnday wrote:haha... i do know something
var $name
is
var $name;
8)
Heh, whoops.