字体:  

问一个类中函数变量问题

jdilt 发表于: 2008-9-06 23:35 来源: PHPChina 开源社区门户

初学php,今天看代码时有一个问题,求助各位,感谢先。。代码如下:
class Example {
    private $catalog = array();
   
    public function SetProperties( $arrayVariables ) {
        foreach ( $arrayVariables as $name => $value ) {
            $this->SetProperty( $name, $value );
        }
    }
   
    public function SetProperty( $name, $value ) {
        $this->$name = $value;
        $this->catalog[] = $name;
    }
   
    public function GetProperties( ) {
        $result = array();
        foreach ( $catalog as $name ) {
            $result[$name] = $this->GetProperty( $name );
        }
        
        return( $result );
    }
   
    public function GetProperty( $name ) {
        return ( $this->$name );
    }
}

上面代码中Example类中的SetProperty函数中的
$this->$name = $value;

这个$name变量并没有在Example类中定义,是为什么?

最新回复

hedgelog at 2008-9-06 23:51:59
执行到这里就定义了,默认Public修饰
异度冰晶 at 2008-9-07 09:37:16
PHP访问一个变量并不需要预先定义~这才是本质