What is the proper way of accessing private/protected variable in a class in PHP?
I learned that it can be access using __construct
function.
Ex.
class helloWorld
{
public $fname;
private $lname;
protected $full;
function __construct()
{
$this->fname = "Hello";
$this->lname = "World";
$this->full = $this->fname . " " . $this->lname;
}
}
Or create a Getter
or Setters
function. I don't know if that's the right term.
class helloWorld
{
public $fname;
private $lname;
protected $full;
function getFull(){
return $this->full;
}
function setFull($fullname){
$this->full = $fullname;
}
}
or via __toString
. I'm confuse o what should I use. Sorry, I'm still new to OOP. Also what is ::
symbol in php and how can I use it?
Thanks :)