Could someone clarify an OOP question for me please?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
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?

Post by impulse() »

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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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";
?>
Each object has its own set of properties (in this case only $bar). If you set $a->bar it does not affect $b->bar.
When you're calling a method of an object it needs to reference its own data somehow, and that's what $this is doing.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

$this is used within classes to say that it is a property or a method of the class:

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"
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.
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post by ibbo »

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

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");
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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

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 :)
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

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:

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";
  }
}
My understanding of this is that I've
  • Created a class
  • Created the variables for use within the class
  • Created a function that access the variables in the class
Am I correct in using

Code: Select all

echo $this->a
? 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I think I've got it!

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";
  }
}
?>
And then my run file:


Code: Select all

<?php

include ("/var/www/html/php/oop/class.php");
$strings = new strings;

echo "$strings->a";
echo "$strings->b";
echo "$strings->c";

?>
And it runs fine. This is my first milestone and I think everything will run smooth from here. Thank you for you help.

Stephen,
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Congratulations. However, one thing. You don't need to enclose your variables in double quotes.

ie

Code: Select all

echo $strings->a;
will work just fine, and will actually be a tiny bit faster to run.

klarinetking
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

impulse() wrote:I think I've got it!

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";
  }
}
?>
And then my run file:


Code: Select all

<?php

include ("/var/www/html/php/oop/class.php");
$strings = new strings;

echo "$strings->a";
echo "$strings->b";
echo "$strings->c";

?>
And it runs fine. This is my first milestone and I think everything will run smooth from here. Thank you for you help.

Stephen,
just a question on this,if i run a command like
showStrings();

will that do the same thing as

Code: Select all

echo "$strings->a";
echo "$strings->b";
echo "$strings->c";
after i include his class file?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I would have to do:

Code: Select all

$foo = new strings;

$foo->showStrings();
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

Code: Select all

showStrings()
in the code. You would need to use my above example.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

so then,obviously,if i put this

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"; 
  } 
}
and then made another page and did :

Code: Select all

testing.php
include("class.php");
showStrings();
would that be correct then?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I don't think so. I only learned how to OOP today though :)

Think it would need to be


Code: Select all

include ("blah.php");
$foo = new strings;

$foo->showStrings();
Please someone correct me if I'm wrong.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

GM wrote:I've yet to be convinced that OOP can do anything procedural code can't.
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. :P

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
Post Reply