this is my code:
<?php
class person {
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
public function get_name() {
return $this->name;
}
protected function set_name($new_name) {
if ($this->name != "Jimmy Two Guns") {
$this->name = strtoupper($new_name);
}
}
}
class employee extends person {
protected function set_name($new_name) {
if ($new_name == "Stefan TRALA") {
$this->name = $new_name;
}
else if ($new_name == "Johnny Fingers") {
person::set_name($new_name);
}
}
function __construct($employee_name) {
$this->set_name($employee_name);
}
}
$empl = new employee;
echo $empl->set_name("Stefan TRALA");
?>
Im getting this warning: Warning: Missing argument 1 for employee::__construct() WHY ? why my echo does not work ? What did I write wrong ? Thank you!
Your constructor takes one argument, so it should be:
$empl = new employee("Stefan TRALA");
Then you can remove echo $empl->set_name()
since it is already called in the constructor.
how to get the name?
add this function:
function get_name(){
return $this->name;
}
then use it:
echo $empl->get_name();
Your class expects the person's name as the first argument:
function __construct($persons_name) {
Yet you call it without:
$empl = new employee;
You should either make the name optional:
function __construct($persons_name = '') {
Or pass it in on creation:
$empl = new employee('Joe');
You have declared a constructor function as part of your class. Constructor functions are called immediately upon class instantiation, please observe:
$empl = new employee;
This is instantiating the class, which will immediately call the following:
function __construct($employee_name) {
$this->set_name($employee_name);
}
Obviously, we need to pass in an argument to this function initially otherwise we will get that error.
$empl = new employee("Stefan TRALA");
If you need to change it later, you can with set_name
, but it must be supplied initially.
Alternatively, you can modify the parent constructor function to expect a null but accept a proper argument:
function __construct($persons_name = null)
Your constructor takes an argument ($employee_name)
So you need to instantiate your employee using $empl = new employee("Stefan TRALA");
But also your echo statement is calling a function with no return value. You need to use the get_name function, not set_name, to return the value.
echo $empl->get_name();
The error in your code is not about setters, you are not specifying the parameter at the constructor.
Your code:
$empl = new employee;
echo $empl->set_name("Stefan TRALA");
Is the same as:
$empl = new employee();
echo $empl->set_name("Stefan TRALA");
The fix:
$empl = new employee("Stefan TRALA");
// this works, but, no longer necesarilly
//echo $empl->set_name("Stefan TRALA");
Remember, every time you use the "new" keyword, you are executing a constructor.
And, constructors are written like this:
$empl = new employee("Stefan TRALA");
But, should be read (not written) as this:
$empl = new employee; $empl->__construct("Stefan TRALA");
The error is not at the "setter".
Cheers.