Jump to content

$this->variable = $variable? Why assign?


Pain

Recommended Posts

Im having trouble understanding the point of assigning certain values to certain variables within the class.

 

Say we have

 

<?php
 
class MyClass {
public $variable;
 
public function getVariable($variable) {
$this->variable = $variable; 
}
 
}

Why assign the value of $variable to $this->variable instead of simply using $variable which has been passed as an argument?

 

Thanks:)

 

Link to comment
https://forums.phpfreaks.com/topic/278445-this-variable-variable-why-assign/
Share on other sites

class MyClass {
public $variable;
 public function setVariable($value) {
  $this->variable = $value;
}
public function getVariable($variable) {
 var_dump($this->variable);
 var_dump($variable);
}
 
}
$test = new MyClass;
$test->setVariable('test');
$test->getVariable();

 

Using setVariable like Psycho suggested, you'll see why you need to assign it to $this.

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.