PHP解析错误:PHP 8.1中出现了意外令牌”public”。

huangapple go评论70阅读模式
英文:

PHP Parse error: syntax error, unexpected token "public" in Error Message On PHP 8.1

问题

以上错误消息是在运行 PHP 8.1 时触发的。

英文:

The above error message is triggered, when running in PHP 8.1.

Haven't been able to spot anything obvious, as to where the code below needs to be changed to eliminate this error message.

 public function process_button()
{
    $sid = zen_session_name() . '=' . zen_session_id();
    $formEntries = $this->buildStandardTransactionDetails();
    $formEntries['SuccessURL'] = str_replace('&', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
    $formEntries['FailureURL'] = str_replace('&', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
    $processButtonString = SagepayUtil::processCryptEntries($formEntries);

    $crypt = SagepayUtil::encryptAndEncode($processButtonString, MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);

    $transaction_type = strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
    $this->errorLog(array(
        array('title' => 'Transaction Type', 'content' => $transaction_type),
        array('title' => 'Submit Data', 'content' => $processButtonString)
    ));

    $process_button_string = zen_draw_hidden_field('VPSProtocol', self::SP_PROTOCOL_VERSION) .
        zen_draw_hidden_field('TxType', $transaction_type) .
        zen_draw_hidden_field('Vendor', MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
        zen_draw_hidden_field('ReferrerID', 'BB5F9F0D-8982-4203-AFD4-AF78017E4B92') .
        zen_draw_hidden_field('Crypt', $crypt);
    return $process_button_string;
}

The error points to the first line of code (as below):

 public function process_button()

Full php file:

<?php
/**
* sagepay form
*
* @package paymentMethod
* @copyright Copyright 2003-2017 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @copyright Portions Copyright Nixak
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License 
V2.0
* @version $Id: Author: zcwilt  June 2017 New in v1.5.5 $
*/
require_once(DIR_FS_CATALOG . DIR_WS_MODULES . 
'payment/sagepay_zc/SagepayBasket.php');
require_once(DIR_FS_CATALOG . DIR_WS_MODULES . 
'payment/sagepay_zc/SagepayCustomer.php');
require_once(DIR_FS_CATALOG . DIR_WS_MODULES . 
'payment/sagepay_zc/SagepayUtil.php');
require_once(DIR_FS_CATALOG . DIR_WS_MODULES . 
'payment/sagepay_zc/sagepay_zc_payment.php');
/**
* Class sagepay_form
*/
class sagepay_zc_form extends sagepay_zc_payment
{
/**
* @var array
*/
protected $sagepayResponse;
/**
*
*/
public function __construct()
{
$this->code = 'sagepay_zc_form';
parent::__construct();
$this->form_action_url = 
'https://live.sagepay.com/gateway/service/vspform-register.vsp';
if (MODULE_PAYMENT_SAGEPAY_ZC_FORM_TEST_STATUS == 'test') {
$this->form_action_url = 
'https://test.sagepay.com/gateway/service/vspform-register.vsp';
}
if ((extension_loaded('suhosin') || defined("SUHOSIN_PATCH"))  && 
ini_get('suhosin.get.max_value_length') < 600) {
$this->title .= '<span class="">' . TEXT_TITLE_SUHOSIN_ERROR . 
'</span>';
$this->description = '<span class="">' . 
TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . '</span><br><br>' . 
$this->description;
if ((extension_loaded('suhosin') || defined("SUHOSIN_PATCH"))  && 
ini_get('suhosin.get.max_value_length') < 600) {
$this->title .= '<span class="">' . TEXT_TITLE_SUHOSIN_ERROR . 
'</span>';
$this->description = '<span class="">' . 
TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . '</span><br><br>' . 
$this->description;
$this->enabled = false;
}
}
/**
* @return string
*/
public function process_button()
{
$sid = zen_session_name() . '=' . zen_session_id();
$formEntries = $this->buildStandardTransactionDetails();
$formEntries['SuccessURL'] = str_replace('&', '&', 
zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
$formEntries['FailureURL'] = str_replace('&', '&', 
zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
$processButtonString = 
SagepayUtil::processCryptEntries($formEntries);
$crypt = SagepayUtil::encryptAndEncode($processButtonString, 
MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);
$transaction_type = 
strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
$this->errorLog(array(
array('title' => 'Transaction Type', 'content' => 
$transaction_type),
array('title' => 'Submit Data', 'content' => 
$processButtonString)
));
$process_button_string = zen_draw_hidden_field('VPSProtocol', 
self::SP_PROTOCOL_VERSION) .
zen_draw_hidden_field('TxType', $transaction_type) .
zen_draw_hidden_field('Vendor', 
MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
zen_draw_hidden_field('ReferrerID', 'BB5F9F0D-8982-4203-AFD4- 
AF78017E4B92') .
zen_draw_hidden_field('Crypt', $crypt);
return $process_button_string;
}
/**
*
*/
public function before_process()
{
global $messageStack;
$sagepay_return_data = SagepayUtil::decodeAndDecrypt($_GET['crypt'], 
MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);
$this->errorLog(array(
array('title' => 'Response Data', 'content' => 
$sagepay_return_data)
));
$this->sagepayResponse = 
SagepayUtil::getResponseTokens($sagepay_return_data);
$status = $this->sagepayResponse['Status'];
if (in_array($status, array('OK', 'REGISTERED', 'AUTHENTICATED'))) {
return;
}
$error_message = $this->getResponseErrorMessage($this- 
>sagepayResponse['Status']);
$payment_error_return = 'ERROR ' . sprintf($error_message, $this- 
>sagepayResponse['StatusDetail']);
$this->errorLog(array(
array('title' => 'Response Values', 'content' => implode("\n", 
$this->sagepayResponse))
));
$messageStack->add_session('checkout_payment', 
$payment_error_return, 'error');
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', 
true, false));
}
/**
* @param $zf_order_id
*/
public function after_order_create($zf_order_id)
{
global $db;
$transactionData = $this->sagepayResponse;
$sagepayTransaction = array();
$sagepayTransaction[] = array('fieldName' => 'vpstxid', 'value' => 
$transactionData['VPSTxId'], 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'zen_order_id', 'value' 
=> $zf_order_id, 'type' => 'integer');
$sagepayTransaction[] = array('fieldName' => 'api_type', 'value' => 
$this->code, 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'vendor_tx_code', 
'value' => $transactionData['VendorTxCode'], 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'transaction_status', 
'value' => $transactionData['Status'], 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'status_detail', 
'value' => issetorArray($transactionData, 'StatusDetail', ''), 
'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'tx_auth_no', 'value' 
=> issetorArray($transactionData, 'TxAuthNo', ''), 'type' => 
'string');
$sagepayTransaction[] = array('fieldName' => 'avs_cv2', 'value' => 
issetorArray($transactionData, 'AVSCV2', ''), 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'address_result', 
'value' => issetorArray($transactionData, 'AddressResult', ''), 
'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'postcode_result', 
'value' => issetorArray($transactionData, 'PostCodeResult', ''), 
'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'cv2_result', 'value' 
=> issetorArray($transactionData, 'CV2Result', ''), 'type' => 
'string');
$sagepayTransaction[] = array('fieldName' => 
'three_d_secure_status', 'value' => issetorArray($transactionData, 
'3DSecureStatus', ''), 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'cavv_result', 'value' 
=> issetorArray($transactionData, 'CAVV', ''), 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'card_type', 'value' => 
issetorArray($transactionData, 'CardType', ''), 'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'last_4_digits', 
'value' => issetorArray($transactionData, 'Last4Digits', ''), 'type' 
=> 'string');
$sagepayTransaction[] = array('fieldName' => 'address_status', 
'value' => issetorArray($transactionData, 'AddressStatus', ''), 
'type' => 'string');
$sagepayTransaction[] = array('fieldName' => 'payer_status', 'value' 
=> issetorArray($transactionData, 'PayerStatus', ''), 'type' => 
'string');
$db->perform(TABLE_SAGEPAY_TRANSACTION, $sagepayTransaction);
}
}

答案1

得分: 0

这个函数不是类的一部分。所以移除 public 关键字,变成如下:

function process_button()

更新

在你编辑了问题之后,问题变得清晰了,有一个语法错误。构造函数的末尾缺少闭大括号 }

public function __construct()
{
    $this->code = 'sagepay_zc_form';
    parent::__construct();
    $this->form_action_url =
        'https://live.sagepay.com/gateway/service/vspform-register.vsp';
    if (MODULE_PAYMENT_SAGEPAY_ZC_FORM_TEST_STATUS == 'test') {
        $this->form_action_url =
            'https://test.sagepay.com/gateway/service/vspform-register.vsp';
    }
    if ((extension_loaded('suhosin') || defined("SUHOSIN_PATCH")) &&
        ini_get('suhosin.get.max_value_length') < 600) {
        $this->title .= '<span class=""></span>' . TEXT_TITLE_SUHOSIN_ERROR .
            '</span>';
        $this->description = '<span class=""></span>' .
            TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . '</span><br><br>' .
            $this->description;
        if ((extension_loaded('suhosin') || defined("SUHOSIN_PATCH")) &&
            ini_get('suhosin.get.max_value_length') < 600) {
            $this->title .= '<span class=""></span>' . TEXT_TITLE_SUHOSIN_ERROR .
                '</span>';
            $this->description = '<span class=""></span>' .
                TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . '</span><br><br>' .
                $this->description;
            $this->enabled = false;
        }
    }
} // <-- 这个地方缺失了

因此,由于这是一个类方法,重新添加 public 关键字如下:

public function __construct()
英文:

This function is not part of a class. So remove the public keyword to be just

function process_button()

Update

After you edited the question is clear, there is a syntax error. The closing brace } is missing at the end of the constructor.

    public function __construct()
{
$this-&gt;code = &#39;sagepay_zc_form&#39;;
parent::__construct();
$this-&gt;form_action_url =
&#39;https://live.sagepay.com/gateway/service/vspform-register.vsp&#39;;
if (MODULE_PAYMENT_SAGEPAY_ZC_FORM_TEST_STATUS == &#39;test&#39;) {
$this-&gt;form_action_url =
&#39;https://test.sagepay.com/gateway/service/vspform-register.vsp&#39;;
}
if ((extension_loaded(&#39;suhosin&#39;) || defined(&quot;SUHOSIN_PATCH&quot;))  &amp;&amp;
ini_get(&#39;suhosin.get.max_value_length&#39;) &lt; 600) {
$this-&gt;title .= &#39;&lt;span class=&quot;&quot;&gt;&#39; . TEXT_TITLE_SUHOSIN_ERROR .
&#39;&lt;/span&gt;&#39;;
$this-&gt;description = &#39;&lt;span class=&quot;&quot;&gt;&#39; .
TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . &#39;&lt;/span&gt;&lt;br&gt;&lt;br&gt;&#39; .
$this-&gt;description;
if ((extension_loaded(&#39;suhosin&#39;) || defined(&quot;SUHOSIN_PATCH&quot;))  &amp;&amp;
ini_get(&#39;suhosin.get.max_value_length&#39;) &lt; 600) {
$this-&gt;title .= &#39;&lt;span class=&quot;&quot;&gt;&#39; . TEXT_TITLE_SUHOSIN_ERROR .
&#39;&lt;/span&gt;&#39;;
$this-&gt;description = &#39;&lt;span class=&quot;&quot;&gt;&#39; .
TEXT_DESCRIPTION_SUHOSIN_GET_LENGTH_ERROR . &#39;&lt;/span&gt;&lt;br&gt;&lt;br&gt;&#39; .
$this-&gt;description;
$this-&gt;enabled = false;
}
}
} // &lt;-- This one is missing

So since it is a class method, re-add the public keyword back in.

答案2

得分: 0

如果您尝试在PHP中的函数前添加**public**关键字,并且出现错误,很可能是因为您没有在类内部使用它。

因此,您在这里也在尝试将公共关键字添加到不在任何类内部的函数中。

解决方法是从函数前面简单地删除**public**关键字,或者将该函数定义在某个类内部,如下所示:


class Test{
    public function process_button()
    {
        $sid = zen_session_name() . '=' . zen_session_id();
        $formEntries = $this->buildStandardTransactionDetails();
        $formEntries['SuccessURL'] = str_replace('&amp;', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
        $formEntries['FailureURL'] = str_replace('&amp;', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
        $processButtonString = SagepayUtil::processCryptEntries($formEntries);

        $crypt = SagepayUtil::encryptAndEncode($processButtonString, MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);

        $transaction_type = strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
        $this->errorLog(
            array(
                array('title' => 'Transaction Type', 'content' => $transaction_type),
                array('title' => 'Submit Data', 'content' => $processButtonString)
            )
        );

        $process_button_string = zen_draw_hidden_field('VPSProtocol', self::SP_PROTOCOL_VERSION) .
            zen_draw_hidden_field('TxType', $transaction_type) .
            zen_draw_hidden_field('Vendor', MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
            zen_draw_hidden_field('ReferrerID', 'BB5F9F0D-8982-4203-AFD4-AF78017E4B92') .
            zen_draw_hidden_field('Crypt', $crypt);
        return $process_button_string;
    }
}

如果您不想在类内部定义函数,请尝试以下代码:


function process_button()
{
    $sid = zen_session_name() . '=' . zen_session_id();
    $formEntries = $this->buildStandardTransactionDetails();
    $formEntries['SuccessURL'] = str_replace('&amp;', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
    $formEntries['FailureURL'] = str_replace('&amp;', '&', zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, 'SSL', false));
    $processButtonString = SagepayUtil::processCryptEntries($formEntries);

    $crypt = SagepayUtil::encryptAndEncode($processButtonString, MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);

    $transaction_type = strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
    $this->errorLog(
        array(
            array('title' => 'Transaction Type', 'content' => $transaction_type),
            array('title' => 'Submit Data', 'content' => $processButtonString)
        )
    );

    $process_button_string = zen_draw_hidden_field('VPSProtocol', self::SP_PROTOCOL_VERSION) .
        zen_draw_hidden_field('TxType', $transaction_type) .
        zen_draw_hidden_field('Vendor', MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
        zen_draw_hidden_field('ReferrerID', 'BB5F9F0D-8982-4203-AFD4-AF78017E4B92') .
        zen_draw_hidden_field('Crypt', $crypt);
    return $process_button_string;
}

在上面的代码中,我只是删除了访问修饰符public

英文:

If you are trying to add the public keyword to a function in PHP and it is giving you an error, it is likely because you are not using it within a class.

So, here you are also doing the same you are adding public keyword to a function that is not inside any class.

So, the solution for this is to simply remove the public keyword from the front of function or to define the function inside some class as follows:

&lt;?php
class Test{
public function process_button()
{
$sid = zen_session_name() . &#39;=&#39; . zen_session_id();
$formEntries = $this-&gt;buildStandardTransactionDetails();
$formEntries[&#39;SuccessURL&#39;] = str_replace(&#39;&amp;amp;&#39;, &#39;&amp;&#39;, zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, &#39;SSL&#39;, false));
$formEntries[&#39;FailureURL&#39;] = str_replace(&#39;&amp;amp;&#39;, &#39;&amp;&#39;, zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, &#39;SSL&#39;, false));
$processButtonString = SagepayUtil::processCryptEntries($formEntries);
$crypt = SagepayUtil::encryptAndEncode($processButtonString, MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);
$transaction_type = strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
$this-&gt;errorLog(
array(
array(&#39;title&#39; =&gt; &#39;Transaction Type&#39;, &#39;content&#39; =&gt; $transaction_type),
array(&#39;title&#39; =&gt; &#39;Submit Data&#39;, &#39;content&#39; =&gt; $processButtonString)
)
);
$process_button_string = zen_draw_hidden_field(&#39;VPSProtocol&#39;, self::SP_PROTOCOL_VERSION) .
zen_draw_hidden_field(&#39;TxType&#39;, $transaction_type) .
zen_draw_hidden_field(&#39;Vendor&#39;, MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
zen_draw_hidden_field(&#39;ReferrerID&#39;, &#39;BB5F9F0D-8982-4203-AFD4-AF78017E4B92&#39;) .
zen_draw_hidden_field(&#39;Crypt&#39;, $crypt);
return $process_button_string;
}
}
?&gt;

If you want dont want to define the function inside a class then try the following code:

&lt;?php
function process_button()
{
$sid = zen_session_name() . &#39;=&#39; . zen_session_id();
$formEntries = $this-&gt;buildStandardTransactionDetails();
$formEntries[&#39;SuccessURL&#39;] = str_replace(&#39;&amp;amp;&#39;, &#39;&amp;&#39;, zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, &#39;SSL&#39;, false));
$formEntries[&#39;FailureURL&#39;] = str_replace(&#39;&amp;amp;&#39;, &#39;&amp;&#39;, zen_href_link(FILENAME_CHECKOUT_PROCESS, $sid, &#39;SSL&#39;, false));
$processButtonString = SagepayUtil::processCryptEntries($formEntries);
$crypt = SagepayUtil::encryptAndEncode($processButtonString, MODULE_PAYMENT_SAGEPAY_ZC_FORM_PASSWORD);
$transaction_type = strtoupper(MODULE_PAYMENT_SAGEPAY_ZC_FORM_TXTYPE);
$this-&gt;errorLog(
array(
array(&#39;title&#39; =&gt; &#39;Transaction Type&#39;, &#39;content&#39; =&gt; $transaction_type),
array(&#39;title&#39; =&gt; &#39;Submit Data&#39;, &#39;content&#39; =&gt; $processButtonString)
)
);
$process_button_string = zen_draw_hidden_field(&#39;VPSProtocol&#39;, self::SP_PROTOCOL_VERSION) .
zen_draw_hidden_field(&#39;TxType&#39;, $transaction_type) .
zen_draw_hidden_field(&#39;Vendor&#39;, MODULE_PAYMENT_SAGEPAY_ZC_FORM_VENDOR_NAME) .
zen_draw_hidden_field(&#39;ReferrerID&#39;, &#39;BB5F9F0D-8982-4203-AFD4-AF78017E4B92&#39;) .
zen_draw_hidden_field(&#39;Crypt&#39;, $crypt);
return $process_button_string;
}
?&gt;

Here, in the above code i just removed the access specifier public

答案3

得分: 0

这与public关键字无关。而是与它之前的代码有关。

__construct()函数缺少闭括号}。因此,在下一个函数之前添加一个},问题应该就解决了。

英文:

This has nothing to do with the public keyword. But to the code just before it.

The __construct() function misses the closing parentheses }. So anything immediately after that would be unexpected keyword. Just add one more } before the next function and things should be fixed.

huangapple
  • 本文由 发表于 2023年5月7日 18:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定