Jump to content

Recommended Posts

I've never understood what this means:  ->

 

For instance, I'll see it used, and I think it typically references functions (maybe?)  for instance $this->my_variable

 

I know it's also used alot in perl.  Does it provide the same function in perl?

 

Can anyone explain this to me in layman's terms?

 

I guess I also need to under what "$this" does?  The php manual defines it as a "pseudo-variable", and I have no idea what that means?

Edited by jdlev
Link to comment
https://forums.phpfreaks.com/topic/278557-what-does-this-do/
Share on other sites

unless you want to use something like PDO...then you'll need to get used to seeing -> about the place.

 

To expand only a little bit on what KevinM1 has stated, the use of $this and -> are tools that only apply in PHP when you are working with objects.  $this is called a psudo-variable as it is used to refference the object that it is within, and does not contain a value in and of it's self - it is dependant on the object that it is inside to five the value. -> is a lot like what it looks like, it lets you drill into an object to call non-static(and perhaps static?) methods (what you casll a functon that is within an object) and parameters (variables that are at the top level of the object). so for example:

//declare the object by declaring a new class:
class thisIsMyObject{
  //make some parameters...
  public $a_parameter;
  public $another_parateter;
  //make a method...
  public funtion setParameter($val1, $val2){
    $this->a_parameter = $val1;
    $this->another_parameter = $val2;
  }
}
 
//create the object :
$myObj = new thisIsMyObject();
 
//assign values to the parameters using the method inside the object:
$myObj->setParameters("first value", "second value");
 
//access the values:
echo "Parameter 1 = {$myObj->a_parameter} and Parameter 2 = ".$myObj->another_parameter;
 
//change the parameter directly:
$myObj->a_parameter = "I changed this directly!";

$this doesn't seem to do much here, except maybe make things a little quicker and shorter.  However, it does become really rather powerfull when you take it up a gear and use inheritnace, so even if you are just starting to touch the surface of PHP OOP you really do want to be getting into the habbit of using it.

Link to comment
https://forums.phpfreaks.com/topic/278557-what-does-this-do/#findComment-1433004
Share on other sites

Object-orientated programming is a feature of PHP since PHP 3, but it got upgraded in PHP 5 to be an actual key feature of PHP and as such, the PHP parser now treats objects differently than it did with PHP 3.

 

As mentioned before, unless you want to write programs with objects, you will most likely never use it.

You'll stumble across the mysqli class with XAMPP, so I do recommend learning about objects.

Link to comment
https://forums.phpfreaks.com/topic/278557-what-does-this-do/#findComment-1433012
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.