Upon invoking tryTest(), the output is
Test thisJo
Why is ‘Test this’ printing first and then Jo. Looks like the constructor is being invoked after the method is invoked. Also the text should print Mary since I am assigning the value in the constructor. But it echoes Jo.
<?php
class cityme {
public $nm = "Jo";
public $state = null;
public $country = null;
function _construct($name, $state, $country) {
// set the name
$this->nm = $name;
echo $this->nm; // this echoes after the printout function is called.. and the value is Jo...
// set the state
$this->state = $state;
// set the country
$this->country = $country;
}
/**
* Print the info
*/
public function printout() {
if($this->nm == null) {
echo "it worked";//does not echo
}
echo "Test this";//echoes first...
echo $this->nm;
echo $this->state;
echo $this->country;
}
}
function tryTest() {
$myCity = new cityme("Mary", "Charlotte", "US");
$myCity->printout();
}
?>
Your constructor is missing an underscore. It should be: