我无法访问子类中的变量

时间:2014-01-22 15:16:06

标签: php

首先,非常感谢

我的问题是我有Page(indexStatic.php)有内容变量返回并影响一些头标签,如包含js文件但是当我启动文件时给出这样的消息: 我想访问indexStatic.php

中的displayControler.php变量
<b>Fatal error</b>:  Uncaught exception 'Exception' with message 'js doesn't exist in this class' in C:\webServer\htdocs\blabla\admin\AdminPanel\core\modelAbstract.php:25
Stack trace:
#0 C:\webServer\htdocs\blabla\admin\AdminPanel\static\indexStatic.php(2): ModelAbstract-&gt;__set('js', '&lt;script src=&quot;re...')
#1 C:\webServer\htdocs\blabla\admin\AdminPanel\core\tools.php(14): include('C:\webServer\ht...')
#2 C:\webServer\htdocs\blabla\admin\AdminPanel\core\displayControler.php(33): tools-&gt;includeFile('/static/indexSt...')
#3 C:\webServer\htdocs\blabla\admin\AdminPanel\core\displayControler.php(28): displayControler-&gt;getContentHtml('/static/indexSt...')
#4 C:\webServer\htdocs\blabla\admin\AdminPanel\core\displayControler.php(18): displayControler-&gt;setUrlTofunc(NULL)
#5 C:\webServer\htdocs\blabla\admin\AdminPanel\index.php(17): displayControler-&gt;showHtml()
#6 {main}
  thrown in <b>C:\webServer\htdocs\blabla\admin\AdminPanel\core\modelAbstract.php</b> on line <b>25</b><br />

indexStatic.php包含在displayControler.php中,让我们看看文件:

indexStatic.php文件:

    <?php
$this->js ='<script src="resources/scripts/editablegrid-2.0.1.js"></script> ';
$this->js .='<script src="resources/scripts/jquery-1.7.2.min.js" ></script>';
$this->js.='<script src="resources/scripts/demo.js" ></script>';
    return'<div class="content-box"><!-- Start Content Box -->
            <script type="text/javascript">
            window.onload = function() { 
                datagrid = new DatabaseGrid();
            }; 
        </script>       

            </div>';
    ?>

displayControler.php

class displayControler extends ModelAbstract
{
    protected $js;
    protected $tools;
    protected $title;
    protected $meta;
    protected $content='';
    protected $urls=array(
            'main'=>'static/aboutStatic.php'
            );

    public function __construct(){
        $this->setTitle(defaultVar::$static['title']);
    }

    public function showHtml(){
        $this->setUrlTofunc(@$_GET['cont']);
        include 'static/adminStatic.php';

    }

    public function setUrlTofunc($url){
        $this->tools=new tools();
        if(isset($url) and isset($this->urls[$url]) ){
            $this->getContentHtml($this->urls[$url]);
        }else{
            $this->getContentHtml(defaultVar::$static['filePath']);
        }

    }
    public function getContentHtml($url){
        $catch = $this->tools->includeFile($url);
        $this->setContent($catch);
    }
}

和modelAbstract.php有:

abstract class ModelAbstract{
    public function __call($name, $args){
        $methodPrefix = substr($name, 0,3);
        $methodProperty = strtolower($name[3]).substr($name,4);

        switch ($methodPrefix){
            case "set":
                if (count($args) == 1)
                    $this->__set($methodProperty,$args[0]);
                else
                    throw new \Exception("magic method only supports one argument");
                break;
            case "get":
                return $this->__get($methodProperty);
                break;
            default:
                throw new Exception("magic methods only supported for get and set");
        }
    }
    public function __set($name,$value){
        if (property_exists($this, $name))
        $this->$name = $value;
        else
            throw new \Exception("$name doesn't exist in this class");
    }
    public function __get($name){
        if (property_exists($this, $name))
            return $this->$name;
        else
            throw new \Exception("$name doesn't exist in this class");
    }


}

1 个答案:

答案 0 :(得分:0)

$ js属性受到保护,因此您无法直接访问它。你在Model类中有魔法,它为你的属性制作了getter和setter。

这意味着它将通过魔术召唤。尝试使用$ this-&gt; setJs(“”)而不是$ this-&gt; js =“”。