Jump to content

$this variable


tarlejh

Recommended Posts

Hi, I need some clarification of a simple O-O concept. 

 

Within a class (in the example below for instance), does the "$this" variable refer to the object, or a does it only refer to a copy of the object?

 

Thanks.

<?php 
class Foo 
{
public $prop;

public function bar(){
  echo $this->prop;
}

} ?>

Link to comment
Share on other sites

btherl's reply brings up some interesting aspects, but I thought I'd expand purely on the question of $this.

 

A class is like DNA.  It's not a thing yet, but it's the blueprint for the thing.

 

You make an object using the "new" keyword.

 

$myFoo = new Foo();

 

So the question in regards to what $this-> does inside class code, is that it represents "this object", allowing you to refer to its variables and methods.  At runtime the values will reflect the object that was created by new.  Of course you can have many Foo() objects, each with its own properties.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.