Can I use string concatenation to define a class CONST in PHP?
Imho, this question deserves an answer for PHP 5.6+, thanks to @jammin comment
Since PHP 5.6 you are allowed to define a static scalar expressions for a constant:
class Foo {
const BAR = "baz";
const HAZ = self::BAR . " boo\n";
}
Although it is not part of the question, one should be aware of the limits of the implementation. The following won't work, although it is static content (but might be manipulated at runtime):
class Foo {
public static $bar = "baz";
const HAZ = self::$bar . " boo\n";
}
// PHP Parse error: syntax error, unexpected '$bar' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS)
class Foo {
public static function bar () { return "baz";}
const HAZ = self::bar() . " boo\n";
}
// PHP Parse error: syntax error, unexpected '(', expecting ',' or ';'
For further information take a look at: https://wiki.php.net/rfc/const_scalar_exprs and http://php.net/manual/en/language.oop5.constants.php
Include constant in string without concatenating
No.
With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.
constant()
is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.
Manual on constants in PHP
How to declare a PHP class constant with concatenation?
I figured I'd post my solution as an answer - just in case no duplicate for the question is found.
For your specific error, the problem is the function call to chr
. Though, to answer the title, concatenation is not currently possible for class const
's.
To solve your particular issue though, you can make use of the back-reference's of \r\n
making your line look like:
const sEOLChars = "\r\n";
OR you can use the built-in PHP_EOL
constant, but be advised it only gives you the end of line constant for your current platform. ^^
Concatenating lang CONST and STR CONST in PHP
Do not do concatenation while declaring class variables.
private $dir_forms = __DIR__ . 'Hola';
// ^ This is NOT allowed during declaration
You can use your constructor function to set such variables.
private $dir_forms;
public function __construct() {
$this -> dir_forms = __DIR__ . 'Hola';
}
How to Concatenate String and Variable with DEFINE in PHP
In order to call a constant with a variable you can use this:
echo constant("M_{$m3}");
How can I concatenate a constant and a variable and store it in a class constant with PHP?
You don't. Constants are constant. You can't store anything in them.
You can use a static property though.
class My_Class {
public static $DB_TABLE;
}
My_Class::$DB_TABLE = TABLE_PREFIX . 'class_table';
You can't do it within the declaration, so you might prefer a static method instead.
class My_Class {
public static function dbTable() {
return TABLE_PREFIX . 'class_table';
}
}
PHP concatenate variable with constant
At this line
$templatePath = dirname( __FILE__ ) . '/' . template;
template
is not constant, because constant template
declared inside class. This code works similar
$templatePath = dirname( __FILE__ ) . '/template';
so, use static::template
PHP string concatenation within class definition
The default values for properties must be constant in the source, e.g. strings and array literals. No expressions.
Use the __construct()
method for anything more complicated.
Related Topics
Composer Cannot Download Files
Remove Double Quote in JSON_Encode()
3Rd Party Dependency Conflict in Developing Wordpress Plugin
How to Set Cron Job Through PHP Script
Accessing Main Picture of Wikipedia Page by API
Twig for Loop for Arrays with Keys
How to Listen to a Tcp Port Using PHP
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
Algorithm for Generating a Random Number
How to Obtain a Nested HTML List from Object's Array Recordset
How to Install Composer on App Service
Php: $_Get and $_Post in Functions
How to Get &Curren to Display Literally, Not as an HTML Entity