解析错误:语法错误,我的PHP API代码中出现意外的'='

时间:2018-07-13 14:37:52

标签: php api

我遇到此错误

  

解析错误:语法错误,出现意外的'='   C:\ xampp \ htdocs \ hrm \ app \ myAPI \ attendance \ punchIn.php在第22行

phnchIn.php

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../../config/Database.php';
include_once '../../models/Attendance.php';

$database = new Database();
$db = $database->connect();

$attendance = new Attendance($db);

// get posted data
// $data = json_decode(file_get_contents("php://input"));
$data = (object) $_POST;

// set attendance property values
$attendance::employee = $data->employee;
$attendance::in_time = $data->in_time;
$attendance::note = $data->note;

// create the attendance
if($attendance->punchIn()){
    echo '{';
        echo '"message": "Attendance was created."';
    echo '}';
}

// if unable to create the attendance, tell the user
else{
    echo '{';
        echo '"message": "Unable to create attendance."';
    echo '}';
}
?>

任何帮助都是很好的帮助

1 个答案:

答案 0 :(得分:0)

您将类属性与静态方法的语法一起使用,这会导致错误。所以...

// set attendance property values
$attendance::employee = $data->employee;
$attendance::in_time = $data->in_time;
$attendance::note = $data->note;

应该是..

// set attendance property values
$attendance->employee = $data->employee;
$attendance->in_time = $data->in_time;
$attendance->note = $data->note;

如果您不熟悉静态方法,则可以使用这些方法而无需实例化类。在您的示例中,它将是Attendance::some_static_method(); Here is more information from the PHP docs.

但是可以使用以下语法访问属性:$attendance->some_property;,据我所知,这就是您想要的。 See the PHP docs for more information on properties.