Home » Php » syntax – replace the $ in php variable declaration

syntax – replace the $ in php variable declaration

Posted by: admin July 12, 2020 Leave a comment

Questions:

I would like to extend the php syntax, in order to tell apart mutable and immutable variables.

$a should be declared mutable (as in standard php)
and #b should be declared immutable.

I’ve read Hacking PHP syntax,

and I couldn’t figure out where can I define that variables declared with # should also be tokenized as T_VARIABLE ?

How to&Answers:

I was able to solve this issue,
Two steps need to be taken:

Make PHP not parse '#' as comments:

Change:

<ST_IN_SCRIPTING>"#"|"//" {

To

<ST_IN_SCRIPTING>"//" {

In line 1901 in zend_language_scanner.l

Tokenize #a as a variable:

Change:

simple_variable:
T_VARIABLE { $$ = $1; }
| '$' '{' expr '}' { $$ = $3; }
| '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
;

To:

simple_variable:
T_VARIABLE { $$ = $1; }
| '$' '{' expr '}' { $$ = $3; }
| '$' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
| '#' '{' expr '}' { $$ = $3; }
| '#' simple_variable { $$ = zend_ast_create(ZEND_AST_VAR, $2); }
;

In line 1117 of zend_language_parser.y

That’s it

Now this code works:

#a=1;
echo (#a);//1

Answer:

My first guess was to see if you could change this in your php.ini file for your project however there is not an option that allows you to change the sign. http://php.net/manual/en/ini.list.php

My best guess would be that you would need to look in the actual PHP code base or ask the PHP community if they would add this feature for you.