调用后tryTest()
,输出为
测试这个乔
为什么先打印“测试这个”,然后再打印 Jo. 看起来在调用方法之后正在调用构造函数。此外,文本应该打印 Mary,因为我在构造函数中分配了值。但它与乔相呼应。
<?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();
}
?>