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;
Related
Is it possible to reference a class constant in an assertion's error message? Everything I tried did not work:
#Assert\GreaterThan(value="foo", message="It is {User::FOO})"
#Assert\GreaterThan(value="foo", message="It is" . User::FOO)"
Results:
Displays It is {User::FOO}) literally.
Throws exception [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got '.'
It is possible to use a constant for the message - you just cannot combine it with custom text. So the workaround is:
#Assert\GreaterThan(value="foo", message=User::ERROR_MESSAGE)
Note: self::ERROR_MESSAGE does not work.
Then define your two constants like this:
const FOO = 'foo';
const ERROR_MESSAGE = 'It is ' . self::FOO;
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');
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.
I believe my class is correct but when I try to echo the output of the class I get an error on line 28: the line " echo 'Your full name ...." is line 28. Any help would be nice
<?php
echo 'Your full name is ' . $person->retrieve_full_name() . '.';
?>
This is where I created the function "retrieve_full_name"
public function __retrieve_full_name() {
$fullname = $this->firstname . ' . ' . $this->lastname;
return $fullname;
}/* This ends the Full Name Function*/
the error I get is
Fatal error: Call to undefined method stdClass::retrieve_full_name() in /home/mjcrawle/processlogin2.php on line 28
your function is called __retrieve_full_name, but you call retrieve_full_name. notice the missing underscores.
double underscores are usually the prefix for php internal/magic functions, i would advise against using them in your function names.
Your immediate error is due to the fact that you call your method by the wrong name. And:
don't use underscores to start a method name
don't use underscores in method names at all, if you care for best practice, use camel casing instead retrieveFullName().
public function retrieve_full_name() {