This is not a question about a particular problem, but rather a question asking for advice and / or assistance.
Im a 2nd year student, Im really struggling to get the hang of OOP programming, the fact that my textbook is not very clear, in its explanation is not helping either. I know there are 100's of examples on the web of this probably but I would like to focus specifically on the example used in my textbook.
The introduction to OOP in php starts with this: EXAMPLE 1
class Person{
var $name;
function set_name($data){
$this->name=$data;
}
function get_name(){
return $this->name;
}
}
$ralph = new Person;
$ralph->set_name('Ralph');
echo 'friend name is '.$ralph->get_name();
Fine I get that no problem However it then goes on giving, what is in my view a very brief explanation on constructors, saying:
Wouldn't it be nice if you could both create and initialize an object at the same time, PHP allows you to do that with constructors.
And then proceeds to give the following example EXAMPLE 2(UNCLEAR...?)
class Person{
var $name;
function __construct($data)
{
$this->name=$data
}
function set_name($data){
$this->name=$data;
}
function get_name(){
return $this->name;
}
}
$dan = new Person;
echo"Friend name is ", $dan->get_name(), ".";
My Question
I would really like to know what is the difference between the two examples above? Also if a beginner practical example could be included it will be enormously appreciated!