Jump to content

$this-> Help


mcloan

Recommended Posts

example:

[code]
<?php
  class someClass {
 
      var content;

      function doSomething() {
          $lines = preg_split('/\r?\n/', $this->content);
      } // end function doSomething
  } // end class someClass

$someThing = new someClass;
//these 2 content variables are 2 different variables with 2 different scopes
$someThing->content = "anything";
$content = "blah";
?>
[/code]
$this->content specifies the $content variable in the class someClass.  Your function doSomething is inside that class, so it's like saying "okay I need the variable $content.  Which one? [i]this[/i] one, right here next to me. Oh okay, the one that holds 'anything' not that other one that holds 'blah' "

when you are outside of the class, you can refer to it as the object->property, like where i had $someThing->content. When you are working inside the class, you can use $this->property, and it knows to refer to the property inside that class.
Link to comment
https://forums.phpfreaks.com/topic/27876-this-help/#findComment-127471
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.