php 解析错误:语法错误,意外的“->”(T_OBJECT_OPERATOR)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15229463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:51:22  来源:igfitidea点击:

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)

phpsyntax-error

提问by Shane Malone

I have a feeling this may be a simple syntax error that I'm just not seeing, as I've had a poke around the code and cannot figure out why I'm getting the above error. The code I'm working on is a tutorial from a SAMS books, on building a calendar class. For the record, I'm running PHP 5.4.7. The below code belongs to an include that I'll show below..

我有一种感觉,这可能是一个我没有看到的简单语法错误,因为我对代码进行了一番检查,但无法弄清楚为什么会出现上述错误。我正在处理的代码是 SAMS 书籍中关于构建日历类的教程。为了记录,我正在运行 PHP 5.4.7。下面的代码属于一个包含,我将在下面展示..

function date_pulldown($name) {
    $this->name = $name;
}

function setDate_global( ) {
    if (!this->setDate_array($GLOBALS['$this->name'])) {
        return $this->setDate_timestamp(time());
    }

    return true;
}

function setDate_timestamp($time) {
    $this->timestamp = $time;
    return true;
}

function setDate_array($inputdate) {
    if (is_array($inputdate) &&
        isset($inputdate["mon"]) &&
        isset($inputdate["mday"]) &&
        isset($inputdate["year"])) {

        $this->timestamp = mktime(11, 59, 59, $inputdate["mon"], $inputdate["mday"], $inputdate["year"]);

        return true;
    }

    return false;       
}

function setYearStart($year) {
    $this->yearstart = $year;
}

function setYearEnd($year) {
    $this->yearend = $year;
}

function getYearStart() {
    if ($this->yearstart < 0) {
        $nowarray = getdate(time());
        $this->yearstart = $nowarray[year]-5;
    }

    return $this->yearstart;
}

function getYearEnd() {
    if ($this->yearend < 0) {
        $nowarray = getdate(time());
        $this->yearend = $nowarray[year]+5;
    }
    return $this->yearend;
}

function output() {
    if ($this->timestamp < 0) {
        $this->$setDate_global();
    }
    $datearray = getdate($this->timestamp);
    $out = $this->day_select($this->name, $datearray);
    $out .= $this->month_select($this->name, $datearray);
    $out .= $this->year_select($this->name, $datearray);
    return $out;
}

function day_select($fieldname, $datearray) {
    $out = "<select name=\"$fieldname"."[mday]\">\n";
    for ($x=1; $x<=31; $x++) {
        $out .="<option value=\"$x\"".($datearray["mday"]==($x)?" SELECTED":"").">".sprintf("%02d", $x)."\n";
    }
    $out .="</select>\n";
    return $out;
}

function month_select($fieldname, $datearray) {
    $out = "<select name=\"$fieldname"."[mon]\">\n";
    for ($x=1; $x<=12; $x++) {
        $out .="<option value=\"".($x)."\"".($datearray["mon"]==($x)?" SELECTED":"")."> ".$this->months[$x-1]."\n";
    }
    $out .="</select>\n";
    return $out;
}

function year_select($fieldname, $datearray) {
    $out = "<select name=\"$fieldname"."[year]\">";
    $start = $this->getYearStart();
    $end = $this->getYearEnd();
    for ($x=$start; $x<$end; $x++) {
        $out .="<option value=\"$x\"".($datearray["year"]==($x)?" SELECTED":"".">$x\n";
    }
    $out .="</select>\n";
    return $out;
}
}
?>

And the code for the display (including) page:

以及显示(包括)页面的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example 12.4 - Using the date_pulldown Class</title>
</head>
<?php
include("date_pulldown.php");
$date1 = new date_pulldown("fromdate");
$date2 = new date_pulldown("todate");
$date3 = new date_pulldown("foundingdate");
$date3->setYearStart(1972);
if (empty($foundingdate)) 
    $date3->setDate_array(array('mday'=>26, 'mon'=>4, 'year'=>1984));
?>
<body>
<form>
    From:<br />
    <?php print $date1->output(); ?><br /><br />
    To:<br />
    <?php print $date2->output(); ?><br /><br />
    Company founded:<br />
    <?php print $date3->output(); ?><br /><br />
    <input type="submit" />
</form>    
</body>
</html>

回答by Tchoupi

You are missing a $sign:

你缺少一个$标志:

if (!this->setDate_array($GLOBALS['$this->name'])) {

should be

应该

if (!$this->setDate_array($GLOBALS['$this->name'])) {

回答by Niet the Dark Absol

if (!this->setDate

This line is missing a $before this, so thisis being treated as a constant, which doesn't exist so is treated as a string, which can't have ->after it.

此行缺少$before this,因此this被视为常量,该常量不存在,因此被视为字符串,后面不能有->

回答by jnthnjns

In addition to the above mentioned if (!thisto if (!$this

除了上面提到if (!thisif (!$this

You also need to revisit this line in the function year_select:

您还需要重新访问函数中的这一行year_select

$out .="<option value=\"$x\"".($datearray["year"]==($x)?" SELECTED":"".">$x\n";

Looks like it should be changed to:
(Note a couple changes I made were to match your formatting from other functions, there doesn't seem to be consistency)

看起来应该更改为:(
注意我所做的一些更改是为了匹配其他函数的格式,似乎没有一致性)

$out .= "<option value=\"".$x."\"".($datearray["year"]==($x)?" SELECTED":"").">".$x."\n";