Hi guys,
I'm making the switch from procedural to OOP and am a bit lost on why we use the __construct. I've watched a few videos and read online tutorials but getting a little confused, so was hoping someone might clear it up for me.
From which I read, the construct is automatically executed or called when an object of a class is created. So does this mean for example using the code below, the construct has set the values for the variables called "first_name", "last_name" and "outstanding_amount". Therefore if I use any of the functions inside of the Customer class then the variables for these 3 will always default to John Jones 20, unless I specify them? I'm guessing not as tried not setting the data (using setData) expecting John Jones 20 to appear, however nothing did. Aynone able to explain this one to me please? Bit lost and gather this is an important one to grasp.
<?php
class Customer {
private $first_name;
private $last_name;
private $outstanding_amount;
public function __construct() {
$first_name = "John";
$last_name = "Jones";
$outstanding_amount = 20;
}
public function setData($first_name, $last_name, $outstanding_amount) {
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->outstanding_amount = $outstanding_amount;
}
public function printData() {
echo "Name : " . $this->first_name . " " . $this->last_name . "\n";
echo "Outstanding Amount : " . $this->outstanding_amount . "\n";
}
}
$c1 = new Customer();
$c1->setData("Tim","Thomas",10);
echo $c1->PrintData();
?>
Thanks in advance.