php 解析错误:语法错误、意外的“const”(T_CONST)、Laravel 项目中的预期变量(T_VARIABLE)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51567998/
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
Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in Laravel project
提问by Arafat Rahman
I have got the following error in my Laravel project after uploading in Bluehost cPanel. But in local server there is no error.
在 Bluehost cPanel 中上传后,我的 Laravel 项目中出现以下错误。但是在本地服务器中没有错误。
Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE)
解析错误:语法错误,意外的“const”(T_CONST),需要变量(T_VARIABLE)
Here is the code
这是代码
<?php
namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Closure;
use Exception;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Driver\PingableConnection;
use Throwable;
use function array_key_exists;
use function array_merge;
use function func_get_args;
use function implode;
use function is_int;
use function is_string;
use function key;
class Connection implements DriverConnection
{
public const TRANSACTION_READ_UNCOMMITTED = TransactionIsolationLevel::READ_UNCOMMITTED;
public const TRANSACTION_READ_COMMITTED = TransactionIsolationLevel::READ_COMMITTED;
public const TRANSACTION_REPEATABLE_READ = TransactionIsolationLevel::REPEATABLE_READ;
public const TRANSACTION_SERIALIZABLE = TransactionIsolationLevel::SERIALIZABLE;
public const PARAM_INT_ARRAY = ParameterType::INTEGER + self::ARRAY_PARAM_OFFSET;
public const PARAM_STR_ARRAY = ParameterType::STRING + self::ARRAY_PARAM_OFFSET;
const ARRAY_PARAM_OFFSET = 100;
protected $_conn;
protected $_config;
protected $_eventManager;
protected $_expr;
private $_isConnected = false;
private $autoCommit = true;
private $_transactionNestingLevel = 0;
private $_transactionIsolationLevel;
private $_nestTransactionsWithSavepoints = false;
private $_params = [];
private $platform;
protected $_schemaManager;
protected $_driver;
private $_isRollbackOnly = false;
protected $defaultFetchMode = FetchMode::ASSOCIATIVE;
My local server PHP version is 7.2.0
我的本地服务器 PHP 版本是 7.2.0
Bluehost PHP version is 7.0.0
Bluehost PHP 版本是 7.0.0
Is that PHP version related problem?
是不是 PHP 版本相关的问题?
How to fix this?
如何解决这个问题?
回答by Nigel Ren
The ability to specify the visibility of class constants was only added in PHP 7.1, from the manual page
指定类常量可见性的功能仅在 PHP 7.1 中添加,来自手册页
Note:
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
笔记:
自 PHP 7.1.0 起,类常量允许使用可见性修饰符。
So on the PHP 7.0 server, the
所以在 PHP 7.0 服务器上,
public const TRANSACTION_READ_UNCOMMITTED ...
lines should not have the public
on them. It also says that
线路上不应有public
。它还说
The default visibility of class constants is public.
类常量的默认可见性是 public。
So public is not needed anyway.
所以无论如何都不需要public。
回答by Nam
It seems like you want to define a constant variable.
看起来你想定义一个常量变量。
My instruction was :
我的指示是:
const CONSTANT = 'jkl';
The error I got :
我得到的错误:
syntax error, unexpected 'const' (T_CONST) in ...
I changed that instruction to :
我将该指令更改为:
define('CONSTANT', 'jkl');
echo CONSTANT;
output :
输出 :
jkl
Syntax of the method 'define' :
'define' 方法的语法:
define('variable_name', 'value_of_the_variable', [case-insensitive_constant_name = true/false]);
For eg.
例如。
define('CONSTANT3', 'ghi', true);
echo CONSTANT3;
define('constant3', 'ghi'); //Defining 'constant3' again will give an error.
But
但
define('CONSTANT3', 'ghi');
echo CONSTANT3;
define('constant3', 'ghi'); //This won't give an error.
echo constant3;
Always remember that regular variables should be addressed by using '$' before their name but constant variables should be addressed directly as shown by me in the code above.
永远记住,常规变量应该在名称前使用“$”来寻址,但常量变量应该直接寻址,如我在上面的代码中所示。