The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:47:31 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: What the hell does $this-> mean?  (Read 5738 times)
Caligula
Rookie
**
Offline Offline

Posts: 39



View Profile
« on: May 04, 2007, 12:27:03 AM »

Ok stupid question.. but its driving me crazy... more and more scripts I pull apart I have been seeing something like this..

$this->info = array();

And for some reason... $this-> part is usually highlighted...which is what is throwing me off... I have tried looking it up.. but I cant find anything.... what is it doing?

Logged
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #1 on: May 04, 2007, 02:04:37 AM »

Ok stupid question.. but its driving me crazy... more and more scripts I pull apart I have been seeing something like this..

$this->info = array();


Quote
A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).

As per http:// php.net/class

So, in the context of your post "info" is a variable (in this case an array) which is a member of a class (object) which $this points to (NOTE: it is an internal pointer, it cannot be used from outside the class scope). Here's an example;

Code:
<?php

class foo
{

    private 
$info;

    function 
set_bar()
    {
       
$this->info "FUBAR";
    }

   function 
print_bar()
   {
       print 
$this->info;

   }
}

// Usage

$my_foobar =& new foo();

$my_foobar->set_bar();

// Will print "FUBAR"
$my_foobar->print_bar();

?>


[edit]Removed moronicness in code.[/edit]

HTH,
td
« Last Edit: May 04, 2007, 02:25:57 AM by thedarkness » Logged

"I want to be the guy my dog thinks I am."
 - Unknown
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: May 04, 2007, 08:38:23 AM »

your not an idiot, i had the same exact question about a week ago for perk.
Another way to explain it is that it is DOT notation.

$this->info = array();

is the same as:

$this.info = array();

info is the method or property
this is the object

though i am not sure if you are setting array or setting $this?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: May 04, 2007, 08:57:30 AM »

though i am not sure if you are setting array or setting $this?

In this case NBs, you are setting a null array pointer to the $this->info member property/reference/variable/whatever you want to call it.

In PHP the garbage collection and variable construction is automatic. So the reason you do this is if you will need to call a function that REQUIRES an array to be created first (like implode()) or if you are cleaning up memory from a previous array. For example, if you had 10K elements in $this->info and wanted to throw them away and free the memory, but still keep info as an array, then by saying $this->info = array() is an easy way to effect that.

Another reason for this notation is that you can both construct and populate the array in one pass. Consider:

$this->info = array(0=>'Hello', 1=>'World');

would create the array info in the current class ($this) and set element [ 0 ] to 'Hello' and [ 1 ] to 'World' . You can do associatives like this as well:

$this->info = array('key1'=>'avalue', 'anotherkey'=>'yet Another Value!');

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #4 on: May 04, 2007, 09:22:26 AM »

oh i get it.

its a constructor. not just a Object.Method call.

$this is a variable initially not a class correct?
info doesnt exists until it is set = the array that you make. array() is just an empty array.
info is more like a "sub variable" correct?
im guessing you can do
$this->somerandomthingy = array() as well right? if so, i get it.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: May 04, 2007, 09:56:26 AM »

its a constructor. not just a Object.Method call.

$this is a variable initially not a class correct?
info doesnt exists until it is set = the array that you make. array() is just an empty array.
info is more like a "sub variable" correct?
im guessing you can do
$this->somerandomthingy = array() as well right? if so, i get it.

@ constructor - precisely. Arrays are just a bit different than primitives and as such, you create them with that function. They'll be auto-created if you were to do something like this:

$myArr[] = 'Hello';
$myArr[] = 'World'

... so that the index reference is auto-incremented. Using array() is just more formal and tells PHP what your intentions are very clearly.

@ $this - right on again. the $this C++ and Delphi equivalent is self. As you know, you'll code using the word $this in the class definition, but the $this context does not exist until an object has been created against that class... and then the $this is a reference to itself. $this can never be used outside of a class definition - it is a reserved word.

@ info: consider the following -

class myClass
{
   private $anInt;
   protected $anotherInt;
   public $yetAnotherInt;
   var $thisVarDefaultsToPublicInt;

   function __construct()
   {
      $this->anInt = 0;
      $this->anotherInt = 1;
      $this->yetAnotherInt = 2;
      $this->thisVarDefaultsToPublicInt=32767;
   }
}

... and now in the normal flow of code...
$myObj = new myClass();
print $myObj->thisVarDefaultsToPublicInt; // prints 32767
print $myObj->yetAnotherInt; // prints 2
print $myObj->anotherInt;
print $myObj->anInt; // Both of these fail because of their visibility in the class

info in the previous examples is just a property or member or whatever their calling a class variable these days.

QQ: Are you a strong OO guy or is this new stuff?

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Caligula
Rookie
**
Offline Offline

Posts: 39



View Profile
« Reply #6 on: May 04, 2007, 04:22:01 PM »

Ahhh OOP & classes...  I haven't gotten that far yet... I head that stuff wasnt for beginners to php and that it was a matter of personal preference...  I personally prefer something easy..lol...  I want to spend more time scheming planning...than coding

But thanks for finally explaining what that is...I can sleep now  Wink
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #7 on: May 04, 2007, 04:31:14 PM »

ah ok. i get it. duh.
info is just a property that was declared in some class. just happens the assignment is inside that class, so $this is the shorthand so you dont have to be reliant upon the class name changing. got it. its a standard oop model.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #8 on: May 04, 2007, 05:59:47 PM »


@ $this - right on again. the $this C++ and Delphi equivalent is self.

Being picky here perk but I'm pretty sure the C++ equivalent of "$this->" is "this->" not "self->" isn't it?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: May 04, 2007, 08:02:32 PM »

You may well be right TD - It's been since the mid 90s that I've even touched C++ - that was Borland 3.1 and it was great, but I am pretty certain that I've forgotten anything pertinent... at least I was clear enough so that you could debug me  Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #10 on: May 10, 2007, 03:40:39 AM »


@ $this - right on again. the $this C++ and Delphi equivalent is self.

Being picky here perk but I'm pretty sure the C++ equivalent of "$this->" is "this->" not "self->" isn't it?

Cheers,
td

Yes this-> is correct
Does PHP have dot notation? if so it may carry the same benefits of dot notation in C++ in that its faster [one less lookup]
However I doubt it Sad
Logged

I am Tyler Durden
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!