bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alias name problem (undefined constant)

sbraaa opened this issue · comments

Recently I moved my project from server which use php 5.5 to another one which use 7.4.3 and I noticed a very strange problem. In html pages I use a lot of aliases:

<a href="{{ ALIASES.my_alias }]">

and this never gaves problem but on php 7.4.3 this cause a notice on apache's error.log :

Use of undefined constant my_alias - assumed 'my_alias' (this will throw an Error in a future version of PHP)

Inspect page using webkit console show me that alias problem translate into a 500 Internal error.
I take a look at php created by f3 in tmp directory I saw that alias call has been translated into :
<a href="<?= (Base::instance()->alias(my_alias)) ?>

As you can see alias name need quotes. Fixing that everything works as expected.
How can I solve this strange behaviour?

At first glance, this looks like an error in your code. ALIASES is a system variable and not a constant. These variables have to be addressed like other variables in the current scope. The @ prefix should be mandatory for all kind of variables:

{{ @ALIASES.my_alias }}

Another solution is to pass the route name to the alias filter:

{{ 'my_alias' | alias }}

Sorry, It was a typo! I'm using @Aliases.
I could try | alias as you suggest but this doesn't explain why the same code runs well on PHP 5.5...
Before Simeone ask: original f3 version used was 3.5, but I've already tried 3.6 and 3.7 without noticed any changes: problems still remain

Older PHP versions raised a NOTICE and not an ERROR when unknown constants were used. Additionally, unknown constants were replaced with strings. You can test this here: https://3v4l.org/3eThj

Because of these types of problems, I register a custom error handler that is upgrading notices to exceptions. More details here: https://www.php.net/manual/en/function.set-error-handler.php#112881

You gave me the right suggest.
I found

{{ my_alias | alias }}

which could run in php 5.5 because constants were replaced with strings (as you remind me!)
Fixing that it seems all runs ok!
Thank you so much!