How do you use $this->variable and return $variable in PHP OOP?
When working with object-oriented programming in PHP, you can use the $this keyword to refer to the current object's properties and methods. To return a variable from within a class, you can use $this->variable to access the class property and return it.
class MyClass {
private $variable;
public function setVariable($value) {
$this->variable = $value;
}
public function getVariable() {
return $this->variable;
}
}
$obj = new MyClass();
$obj->setVariable("Hello, world!");
echo $obj->getVariable(); // Output: Hello, world!