-1

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

1 回答 1

2

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

function __construct($name, $state, $country) {
于 2012-06-07T03:56:59.587 回答