How does PHP handle values within and outside of methods in Object-Oriented Programming?
In Object-Oriented Programming in PHP, values within methods are typically accessed using the $this keyword, which refers to the current instance of the class. Values outside of methods can be accessed using class properties or static variables. To ensure proper encapsulation and data hiding, it is recommended to use access modifiers like public, private, or protected.
class Example {
private $value; // private property
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$example = new Example(10);
echo $example->getValue(); // Outputs: 10