在方法之后调用的构造函数


Constructor getting invoked after a method

调用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();
}
?>

构造函数缺少下划线。它应该是:

function __construct($name, $state, $country) {