I'm trying to create a little enum and I'm just stuck: Why doesn't this work?
class.LayoutParts.php:
<?php
class LayoutParts {
const MAIN = 1;
const FOOTER = 2;
}
?>
class.SupportedLayouts.php:
<?php
class SupportedLayouts {
const MAIN = LayoutParts::MAIN;
const MAIN_FOOTER = LayoutParts::MAIN.LayoutParts::FOOTER;
}
?>
It results the following message:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in /*****/class.SupportedLayouts.php on line 4
Thanks for your help!
Regards, Flo
. is an operator, making LayoutParts::MAIN.LayoutParts::FOOTER; a statement, which isn't allowed in a const or property declaration.
See here
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
Related
I have following class:
class RemoteService {
const BASE_URL = "...";
const SEARCH_URL = self::BASE_URL . "search";
const MIN_PAGE_SIZE = 0;
const MAX_PAGE_SIZE = 100;
const ERROR_INVALID_PAGE_SIZE = sprintf('page size must be between %d and %d', self::MIN_PAGE_SIZE, self::MAX_PAGE_SIZE);
...
}
I can use BASE_URL while initializing SEARCH_URL. However, I cannot use MIN_PAGE_SIZE and MAX_PAGE_SIZE in sprintf() to initialize ERROR_INVALID_PAGE_SIZE and get the following error:
Fatal error: Constant expression contains invalid operation
What is the reason for this? Is concatenation as in first part only way to overcome this?
You have to use concatenation. The documentation says:
The value must be a constant expression, not (for example) a variable, a property, or a function call.
So you can't call sprintf().
Example:
const USERNAME_MIN_LENGTH = '2';
private $uesrname_error_message= 'ERROR: Max. username length is ' . USERNAME_MIN_LENGTH;
I get this error with the code provided above:
Parse error: syntax error, unexpected '.', expecting ',' or ';'
The error occurs for the line in which the $test variable is defined.
I'm using PHP 5.5.12 version.
Why?
Expressions in property declarations are not (yet) supported by PHP. Some expressions will be allowed from PHP 5.6 onwards. You can read more about it on PHP's wiki.
Your specific example should work in PHP 5.6.
You need to define variable $test inside the constructor like this:
class yourClass{
private $test;
function __construct(){
const USERNAME_MIN_LENGTH = '2';
$this->test = 'Max. username length is ' . USERNAME_MIN_LENGTH;
}
}
This only works in PHP 5.6:
private $test = 'Max. username length is ' . USERNAME_MIN_LENGTH;
Currently i am learning php. Here I have a confusion this is my php code
class OBJECT_ENUM
{
const USER = 10;
const POST = 30;
const SECURE_REQUEST = 40;
}
class OPERATION_ENUM
{
const INSERT_USER = OBJECT_ENUM::USER + 1; // <- here it gives an error
const SEND_MAIL = OBJECT_ENUM::USER + 2;
const LIKE_POST = OBJECT_ENUM::POST + 1;
const INSERT_POST = OBJECT_ENUM::POST + 2;
const ENCRYPT = OBJECT_ENUM::SECURE_REQUEST + 1;
}
error message:
Parse error: syntax error, unexpected '+', expecting ',' or ';' in /var/www/workspace/6thAssignment/include/tempCall.php on line 15
I just don't understand why this error occurs.?? cany anybody explain me.??
Thank you in advance
ORIGINAL ANSWER:
As you can see in http://www.php.net/manual/en/language.oop5.constants.php:
The value must be a constant expression, not (for example) a
variable, a property, a result of a mathematical operation, or a
function call.
UPDATED:
From PHP version 5.6 now it is possible to use expressions in constants.
I think you can not do mathematical operation to be assigned to a const variable. Try changing
const INSERT_USER = OBJECT_ENUM::USER + 1;
to
$INSERT_USER = OBJECT_ENUM::USER + 1;
I believe expressions (like $a + 1) are not allowed in constants definitions, so there is why you are getting that error.
At the moment this is not allowed by PHP. There was an RFC (Request for comments) to get this added to the language:
https://wiki.php.net/rfc/const_scalar_expressions
however this was withdrawn as the author of the RFC has left the internals development team. So I suspect this may not happen any time soon but that's not to say that this won't come back in some form or another at a later date.
Can classes have bitshifted values defined as class constants / static variables ?
I want to implement a permission system similar to the one described here http://www.litfuel.net/tutorials/bitwise.htm (sorry best example I could find from a quick google)
For example ive tried this
class permissions {
const perm1 =1;
const perm2 =2;
const perm3 =4;
//--CUT--
const perm24 =8388608;
const perm25 = perm1 | perm2;
} which gives
syntax error, unexpected '|', expecting ',' or ';'
and the preferred way
class permissions {
static $perm1 =1<<0;
static $perm2 =1<<1;
static $perm3 =1<<2;
//--CUT--
static $perm24 =1<<23;
static $perm25 = $perm1 | $perm2;
}
which gives
syntax error, unexpected T_SL, expecting ',' or ';'
The latter way works outside of a class environment eg
$perm1 =1<<0;
$perm2 =1<<1;
$perm3 =1<<2;
//--CUT--
$perm24 =1<<23;
$perm25 = $perm1 | $perm2;
echo $perm25;
Giving the expected 3 (2+1) (or 2^0 + 2^1)
What is the best way to define this in a class ?
Quoting from the docs:
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
bitwise or logical operations qualify (like mathematical operations) as not permitted
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse error: syntax error, unexpected ‘.’, expecting ‘,’ or ‘;’
I have this class:
<?php
class MyClass {
const DB_NAME = "MyDb";
const HOST = "localhost";
const USER = "abcdef";
const PASSWORD = "ghijklmn";
public static $MyString = file_get_contents('file.txt');
}
?>
I have no idea what is wrong with file_get_contents ?
I cannot understand what is the error says ? Why ( is unexpected ?
I read the following articles but these don't help me to solve that error:
Parse error: syntax error, unexpected T_STRING in php
Parse error T_Variable
file_get_contents shows unexpected output while reading a file
It's because you have assigned expression to variable declaration. It can only use constants.
The workaround would be like this
<?php
class MyClass {
...
public static $MyString;
...
}
MyClass::$MyString = file_get_contents('file.txt');