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

时间:2012-06-07 03:50:59

标签: php

调用tryTest()后,输出为

  

测试thisJo

为什么'先测试'打印然后再打印。看起来在调用方法后调用构造函数。由于我在构造函数中赋值,因此文本应该打印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();
}
?>

1 个答案:

答案 0 :(得分:2)

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

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