PHP类没有认识到这个 - >方法()

时间:2015-10-03 01:54:59

标签: php

当尝试从同一个类中的另一个方法中访问我在PHP类中定义的方法时,我收到以下错误:

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /var/www/ ... /TOC.php on line 57

以前,我将这些功能放在任何课堂之外,只是将它们包含在我需要的地方。将它们移动到一个之后,起初我认为我必须遇到问题this user,但是对于类的实例,我可以从另一个文件中调用我的render方法而没有任何问题。注释掉this->printTreeArray($sectionProjects);行消除了错误。这是我的班级:

<?php

class TOC{

    private function printTreeArray($sectionProjects){
        echo "var TOCnodes = [\n";

        //Print each section, with another loop to print each one's problems
        $i = 0;
        foreach($sectionProjects as $sectionProject){
            if($i != 0){
                echo ",\n";
            }

            $project = $sectionProject->getProject();
            //Get due date for mouseover text
            $due = $project->getDueDate('F jS, Y h:i A');

            $q = new ProjectProblemQuery();
            $projectProblems = $q->findByRelProjectId($project->getId());

            $pId = $i + 1;
            echo "{id: $pId, pId: 0, name: \"Project $pId\", title: \"Due: $due\", isParent: true, open: true}";

            //Print this section's problem list
            $probId = 1;
            foreach($projectProblems as $projectProblem){
                echo ",\n";

                $nodeId = ($pId * 10) + $j;

                echo "{id: $nodeId, pId: $pId, name: \"$pId.$probId\", url: \"#\", target: \"_top\"}";

                $j++;
            }

            $i++;
        }
        echo"];\n";
    }

    public function render($section){
        if(! $section instanceof Section){
            echo "$section is not an instance of Section!";
        }
        else{
            echo "<ul id=\"TOCtree\" class=\"ztree\"></ul>\n";

            echo "<script type=\"text/javascript\">\n";

            $sectionProjects = $section->getSectionProjects();

            this->printTreeArray($sectionProjects);

            echo "$(document).ready(function () {
                    $.fn.zTree.init($(\"#TOCtree\"), setting, TOCnodes);
                });";
            echo "</script>";
        }
    }


}

这是我第一次提出问题 - 通常我已经能够找到我的编码难题的答案,但这个让我感到困惑。我意识到这堂课还有很多不优雅的东西,所以如果有一种风格,我可以用来避免将来出现这种错误,我非常感谢学习它。

1 个答案:

答案 0 :(得分:3)

您应该使用$this代替this