Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I have got the following error in my Laravel project after uploading in Bluehost cPanel. But in local server there is no error.

Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (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

Bluehost PHP version is 7.0.0

Is that PHP version related problem?

How to fix this?

like image 938
Arafat Rahman Avatar asked Jul 28 '18 04:07

Arafat Rahman


2 Answers

The ability to specify the visibility of class constants was only added in PHP 7.1, from the manual page

Note:

As of PHP 7.1.0 visibility modifiers are allowed for class constants.

So on the PHP 7.0 server, the

public const TRANSACTION_READ_UNCOMMITTED ...

lines should not have the public on them. It also says that

The default visibility of class constants is public.

So public is not needed anyway.

like image 170
Nigel Ren Avatar answered Oct 17 '22 00:10

Nigel Ren


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('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.

like image 29
Nam Avatar answered Oct 17 '22 00:10

Nam