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;
Related
I have a model called Treatment in CodeIgniter. I want to load and use this model 'dynamically'. That is, I don't want to have to call it directly by name (I am trying to generalize some code to use whatever model I tell it).
So, I do this:
$namespace = 'blah';
$modelName = 'Treatment';
...
$this->load->model($namespace . '/' . $modelName);
$this->model = $this->$$modelName;
However, I get an error when accessing the $this->$$modelName variable, saying that the variable 'Treatment' is undefined:
Undefined variable: Treatment ... Fatal error: Cannot access empty
property in /mydir/application/controllers/rest/base_rest.php on line
202.
Where line 202 is the line where I am using the $this->$$modelName variable.
Now, if I changed line 202 to be:
$this->model = $this->Treatment;
It works fine.
Does anyone know why I can't seem to use the PHP $$ syntax here?
You can't because it's not a supported syntax. Try
$this->{$modelName}
instead. e.g.
php > class foo { public $bar = 42; }
php > $x = new foo();
php > $y = 'bar';
php > echo $x->$$y;
PHP Notice: Undefined variable: bar in php shell code on line 1
PHP Fatal error: Cannot access empty property in php shell code on line 1
php > echo $x->{$y};
42php >
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;
Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.
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() {