当试图从同一类中的另一个方法中访问我在PHP类中定义的方法时,我得到以下错误:
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /var/www/
…/TOC.php on line 57
以前,我把这些函数放在任何类的外部,只在需要的地方包含它们。将它们移到一个文件中后,起初我认为我一定遇到了这个用户遇到的问题,但是有了类的实例,我可以从另一个文件调用我的呈现方法而不会出现问题。注释掉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>";
}
}
}
这是我第一次问关于SO的问题——通常我都能找到我编码困境的答案,但是这个问题让我困惑。我意识到这门课上有很多不太优雅的东西,所以如果将来有什么可以避免这种错误的风格,我非常希望能学会。
您应该使用$this
而不是this
。