Can't declare class constant nor property variable - php

class Model {
public $DATABASE_NAME = 'dealer-kunde-skuska';
// OR
const DATABASE_NAME = 'dealer-kunde-skuska';
}
None of above declarations works. It returns error:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in ... on line 5
What could be wrong?

public and const are only available as of PHP 5. var is used in PHP 4.
Support for PHP 4 has been discontinued since 2007-12-31. Please consider upgrading to PHP 5.
Upgrade maybe? PHP 5 has been around for 11 years and PHP 4 discontinued for 7.

Related

How to fix Syntax error, unexpected '' (T_STRING)?

The error that I am receiving is this: Parse error: syntax error, unexpected 'TokenType' (T_STRING) in C:\Program Files\Ampps\www\cs5339\lepichardo\4339_f22_assignment1\TokenType.php on line 2. To be clear, both files are php, and they both are in the same director. The code below is where I include the enum TokenType so that I would be able to use it.
<?php
include 'TokenType.php'
?>
The TokenType.php looks like this
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
?>
As far as I now, this should be fine, but gives me that error in that particular line.
If there is any other way to declare and imlpement the enum variable in php I would like to be explained to how to. Thanks in advance!!!
enums are only available in PHP 8.1+. The error suggests you are running PHP 7.
Here's some results of running the following code using various PHP versions (using https://3v4l.org/qHU4f)
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
echo 'hello world';
Output for 8.1.0 - 8.1.11, 8.2rc1 - rc3
hello world
Output for 8.0.1 - 8.0.24
Parse error: syntax error, unexpected identifier "TokenType" in /in/qHU4f on line 2
Process exited with code 255.
Output for 7.4.0 - 7.4.32
Parse error: syntax error, unexpected 'TokenType' (T_STRING) in /in/qHU4f on line 2
Process exited with code 255.
As you can see by the last result, unexpected 'TokenType' (T_STRING) is an error you would receive in PHP 7

Nullable string type hinting gives syntax error in PHP 7.2

I'm using PHP 7.2 and trying to type hint this variable:
class TestClass
{
public static ?string $test = null;
But PHP gives me the following error:
syntax error, unexpected '?', expecting function (T_FUNCTION) or const
(T_CONST)
And I'm absolutely clueless why! Am I doing something wrong?
Typed properties were not included until PHP 7.4.

Parse error: syntax error, unexpected T_STRING on namespace string [duplicate]

This question already has answers here:
Getting a syntax error unexpected T_STRING for namespace line
(2 answers)
Closed 3 years ago.
Error:
Parse error: syntax error, unexpected T_STRING in ...Ping.php on line 3
In code:
<?php
namespace JJG; // line 3
class Ping {
private $host;
private $ttl;
//...
}
I expect the class will work fine, as provided https://github.com/geerlingguy/Ping
But the actual output is:
Parse error: syntax error, unexpected T_STRING in ...Ping.php on line 3
OK, I take it.
On this server PHP Version can be only 5.2.5, and seems like there is no full support of namespaces in it yet.

Unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR

I'm trying to run a WordPress plugin, and I get the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION
or T_FUNCTION or T_VAR or '}' in
/nfs/c03/h05/mnt/52704/domains/creathive.net/html/wp-content/plugins/qr-code-tag/lib/qrct/QrctWp.php
on line 13
What would cause this error? Line 13 is the public bit.
EDIT: Here is some code:
class QrctWp
{
public $pluginName = 'QR Code Tag';
Running on PHP4 by any chance? That's what the error message at this location would usually indicate.
Remove all public and private attributes. Though it's unlikely the plugin will work perfectly with the older object instance handling.

PHP Bug? With Class?

Here is my code:
<?php class Video { protected $_test; } ?>
and when I try to include the file containing this code I've got that error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in ../classes/Video.class.php on line 1
What's wrong? I don't understand.
This looks like you're still running PHP 4, which doesn't know the protected keyword.
An update to PHP 5 would be a good idea.

Categories