I LOVE OOP
Moderator: General Moderators
I LOVE OOP
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.
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.
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
haha... Im still in the staging of learning php code.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...
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....
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.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....
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>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>";
}
}
?>- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
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
My project now over the next week is to convert
rest of my code to OOP then i can relax and smile a bit!
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
My project now over the next week is to convert
rest of my code to OOP then i can relax and smile a bit!
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
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
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
Guess im having trouble understanding what the use of the
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
Is $this-> used for like.. if you want to have a function specific meaning
inside the class?
Code: Select all
$this->foo = $bar;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
Is $this-> used for like.. if you want to have a function specific meaning
inside the class?
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
It's a matter of scope, I think, if i read your post right...
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...
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.
Code: Select all
var $bar;Code: Select all
$this->foo = $bar;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
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!).
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!).