Could someone clarify an OOP question for me please?
Moderator: General Moderators
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
Could someone clarify an OOP question for me please?
I've just stepped into OOP and I don't fully understand the layout for OOP code. One thing inparticular is something I've saw crop up in every class which is $this->x. Is the '$this' part stating that it should be adding data to the variable 'x' that's defined within that class? If so, do you delcare variables outside of the class without '$this->' ?
Thanx in advance, Ste
Thanx in advance, Ste
Code: Select all
<?php // php5
class Foo{
public $bar;
public function xyz($i) {
$this->bar = $i;
}
}
$a = new Foo;
$a->bar = 1; // using the object reference to set its property
$b = new Foo;
$b->bar = 2;
echo $a->bar, ' | ', $b->bar, "<br />\n";
$a->xyz(3);
echo $a->bar, ' | ', $b->bar, "<br />\n";
?>When you're calling a method of an object it needs to reference its own data somehow, and that's what $this is doing.
$this is used within classes to say that it is a property or a method of the class:
In this example, we see that from within the foo class, we reference the set_bar method by $this->set_bar(), and we reference the bar property by $this->bar. ie: to call the method set_bar from within the class we need to use $this->set_bar(), and not just set_bar().
Outside the class, we can access these methods by $myFoo->set_bar() and $myFoo->bar, because we've declared $myFoo as a new foo object.
Code: Select all
class foo {
public $bar;
function __construct($value) {
$this->set_bar($value);
}
function set_bar($value) {
$this->bar = $value;
}
}
$myFoo = new foo("Hello");
echo $myFoo->bar;
//outputs "Hello"Outside the class, we can access these methods by $myFoo->set_bar() and $myFoo->bar, because we've declared $myFoo as a new foo object.
To take it a little further.
$this allows you to enssure that you are dealing with this particular instance of a class.
If you had
Using "this" we can see that each call references that variable to that instance of the object. Without it we would run into issues when we try to retrieve a variable. The problem been which instance of the variable.
ibbo
$this allows you to enssure that you are dealing with this particular instance of a class.
If you had
Code: Select all
class A{
public $name;
public function setName($n){
$this->name = $n;
}
}
$a = new A();
$a->setName("bill");
$b = new A();
$b->setName("John");ibbo
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I've picked up PHP & MySQL as quick as can be expected but classes are just one of those things I keep going over and over tutorials of and it's not clicking. Are classes so hard to learn for all programmers when entering into programming? I will keep reading and testing untill I understand it, I'm just not sure if I'll ever understand it 
It's not the classes themselves that are difficult, it's the concept of OOP which can be difficult to someone coming from a procedural background (like me).
It took me quite a while to understand what the point was of using classes at all, and I kept looking at websites like this one, and other OOP websites, trying to understand what was so wonderful about OOP, and how it was going to change my life.
Now I write mainly OOP code, but am still to be convinced that it is the "be all and end all" of programming. I can see where it has advantages (the main two being cited as encapsulation and reusability), but any half decent procedural programmer can write reusable code, and the scope of new procedures and functions is often closed in other languages anyway, which means the encapsulation comes automatically. I guess it's more to do with discipline, and getting work done in a group environment.
I like some of the OOP ideas - I've just started looking at some design patterns, and can see why they could be used, but, I've yet to be convinced that OOP can do anything procedural code can't.
It took me quite a while to understand what the point was of using classes at all, and I kept looking at websites like this one, and other OOP websites, trying to understand what was so wonderful about OOP, and how it was going to change my life.
Now I write mainly OOP code, but am still to be convinced that it is the "be all and end all" of programming. I can see where it has advantages (the main two being cited as encapsulation and reusability), but any half decent procedural programmer can write reusable code, and the scope of new procedures and functions is often closed in other languages anyway, which means the encapsulation comes automatically. I guess it's more to do with discipline, and getting work done in a group environment.
I like some of the OOP ideas - I've just started looking at some design patterns, and can see why they could be used, but, I've yet to be convinced that OOP can do anything procedural code can't.
-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I'm sorry to have to treat this like a classroom but I can't see any other way to understand classes. I understand what classes are for and how they make a programmers life easier, I just don't understand the '->' part of classes. I have at the moment a class file:
My understanding of this is that I've? The reason I've put that is because the function is within the class so I only need to use "$this-><var name>" because it's within the class.
Code: Select all
class strings {
var $a = "Hello";
var $b = ", my name is";
var $c = "Stephen";
function showStrings() {
echo "$this->a $this->b $this->c";
}
}- Created a class
- Created the variables for use within the class
- Created a function that access the variables in the class
Code: Select all
echo $this->a-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I think I've got it!
I have my class file:
And then my run file:
And it runs fine. This is my first milestone and I think everything will run smooth from here. Thank you for you help.
Stephen,
I have my class file:
Code: Select all
<?php
class strings {
var $a = "Hello";
var $b = ", my name is";
var $c = "Stephen";
function showStrings() {
echo "$this->a $this->b $this->c";
}
}
?>Code: Select all
<?php
include ("/var/www/html/php/oop/class.php");
$strings = new strings;
echo "$strings->a";
echo "$strings->b";
echo "$strings->c";
?>Stephen,
-
klarinetking
- Forum Commoner
- Posts: 59
- Joined: Mon Jul 24, 2006 9:43 am
Congratulations. However, one thing. You don't need to enclose your variables in double quotes.
ie
will work just fine, and will actually be a tiny bit faster to run.
klarinetking
ie
Code: Select all
echo $strings->a;klarinetking
just a question on this,if i run a command likeimpulse() wrote:I think I've got it!
I have my class file:
And then my run file:Code: Select all
<?php class strings { var $a = "Hello"; var $b = ", my name is"; var $c = "Stephen"; function showStrings() { echo "$this->a $this->b $this->c"; } } ?>
And it runs fine. This is my first milestone and I think everything will run smooth from here. Thank you for you help.Code: Select all
<?php include ("/var/www/html/php/oop/class.php"); $strings = new strings; echo "$strings->a"; echo "$strings->b"; echo "$strings->c"; ?>
Stephen,
showStrings();
will that do the same thing as
Code: Select all
echo "$strings->a";
echo "$strings->b";
echo "$strings->c";-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I would have to do:
For it to execute, I think. 
** Edit **
Sorry I just re-read your post, from what I understand, yes, it will do exactly the same, but you couldn't just put
in the code. You would need to use my above example.
Code: Select all
$foo = new strings;
$foo->showStrings();** Edit **
Sorry I just re-read your post, from what I understand, yes, it will do exactly the same, but you couldn't just put
Code: Select all
showStrings()so then,obviously,if i put this
and then made another page and did :
would that be correct then?
Code: Select all
class.php
class strings {
var $a = "Hello";
var $b = ", my name is";
var $c = "Stephen";
function showStrings() {
echo "$this->a $this->b $this->c";
}
}Code: Select all
testing.php
include("class.php");
showStrings();-
impulse()
- Forum Regular
- Posts: 748
- Joined: Wed Aug 09, 2006 8:36 am
- Location: Staffordshire, UK
- Contact:
I don't think so. I only learned how to OOP today though
Think it would need to be
Please someone correct me if I'm wrong.
Think it would need to be
Code: Select all
include ("blah.php");
$foo = new strings;
$foo->showStrings();Nobody is saying OOP can do anything procedural can't. It's a matter of it being a more organized and intuitive method for some programmers. For others, not so much. I find it a matter of preference... but... let's not get into this debate again, as it's not what the thread was about.GM wrote:I've yet to be convinced that OOP can do anything procedural code can't.
To impulse()...
Hang in there man... OOP is difficult to grasp at first, but once it clicks, it really clicks. I just started to really understand and use OOP about 3 or 4 months ago, and about a month after I started, it just seemed to sort of "click" one day. After that, the concepts were so exciting and interesting to me, it became sort of an obsession to learn as much about OOP as possible. I was posting questions about objects/classes/patterns several times a day on these boards for a while there... luckily there are so many helpful OOP gurus to help you as you learn