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.
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().
I am trying to implement a logging library which would fetch the current debug level from the environment the application runs in:
23 $level = $_SERVER['DEBUG_LEVEL'];
24 $handler = new StreamHandler('/var/log/php/php.log', Logger::${$level});
When I do this, the code fails with the error:
A valid variable name starts with a letter or underscore,followed by any number of letters, numbers, or underscores at line 24.
How would I use a specific Logger:: level in this way?
UPDATE:
I have tried having $level = "INFO" and changing ${$level} to $$level. None of these changes helped.
However, replacing the line 24 with $handler = new StreamHandler('/var/log/php/php.log', Logger::INFO); and the code compiles and runs as expected.
The variable itself is declared here
PHP Version => 5.6.99-hhvm
So the answer was to use a function for a constant lookup:
$handler = new StreamHandler('/var/log/php/php.log', constant("Monolog\Logger::" . $level));
<?php
class Logger {
const MY = 1;
}
$lookingfor = 'MY';
// approach 1
$value1 = (new ReflectionClass('Logger'))->getConstants()[$lookingfor];
// approach 2
$value2 = constant("Logger::" . $lookingfor);
echo "$value1|$value2";
?>
Result: "1|1"
I wrote an error handler to handle different kind of errors in php(even parse errors etc.).
Question:
As I now can detect an errortype(the constant) it is necessary to identify which errors I should allow or not and in which case do a gentle shutdown.
If I look at http://www.php.net/manual/en/errorfunc.constants.php I see all the different constants for different kind of errors.
The question is:
1)Is there some kind of relation between these constants for error handling. Lets say above a level I know that I dont want to print the error on my screen etc? Or do I have to set this manually for every error-constant(seems like it)?
2) How to provoke every error on the screen example without using trigger_error() or user_error()? Is there some kind of list to produce those errors and which ones I can produce with code?
Cheers and thanks a lot for answers.
You can group all the notice, warning and error constants together like this:
notice : 8 + 1024 + 2048 + 8192 + 16384 = 27656 0x6c08
warning: 2 + 32 + 128 + 512 = 674 0x2a2
error : 1 + 16 + 64 + 256 + 4096 = 4433 0x1151
You could also add them by explicitly using the constant names, e.g. E_ERROR, etc.
So:
$is_notice = $code & 0x6c08 != 0;
$is_warning = $code & 0x2a2 != 0;
$is_error = $code & 0x1151 != 0;
As for your second question, are you looking for code that would trigger the above distinct levels?
$f = fopen($a, 'r'); // notice + warning
$f->read(); // error
include 'script_with_parse_error.php'; // e_parse
function test(Iterator $i) { }
test(123); // e_recoverable_error
function modify(&$i) { ++$i; }
modify(123); // e_strict
$x = split(',', ''); // e_deprecated
The E_USER events can only be generated with trigger_error() of course.
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
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.