在smarty3中,模板目录$template_dir和编译目录$compile_dir都是用protected修饰的,为什么在类外面可以直接访问呢呢??我把源代码中的protected改成private居然还可以访问,这是为什么呢?

解决方案 »

  1.   


    Smarty类中是这样定义这两个属性的(复制粘贴过来的):
     protected $template_dir = null;
     protected $compile_dir = null;
    这是我写的测试代码:
            define(ROOT,"C:/www/apache2/htdocs/smarty/");
    include ROOT."libs/Smarty.class.php";//包含模板类文件
    $smarty = new Smarty();//创建模板对象
    echo "编译目录:".$smarty->compile_dir."<br>";
    echo "模板目录:".$smarty->template_dir[0]."<br>";
    这是输出结果:编译目录:.\templates_c\
    模板目录:.\templates\
      

  2.   

    他不是有这样的定义吗?    public function __get($name)
        {
            $allowed = array(
            'template_dir' => 'getTemplateDir',
            'config_dir' => 'getConfigDir',
            'plugins_dir' => 'getPluginsDir',
            'compile_dir' => 'getCompileDir',
            'cache_dir' => 'getCacheDir',
            );        if (isset($allowed[$name])) {
                return $this->{$allowed[$name]}();
            } else {
                trigger_error('Undefined property: '. get_class($this) .'::$'. $name, E_USER_NOTICE);
            }
        }