# userland macros

- Thread: https://externals.io/message/4068
- Original archive: https://news-web.php.net/php.internals/4068
- Started by Cristiano Duarte on 2003-08-18T18:56:56+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4068 — Cristiano Duarte — 2003-08-18T18:56:56+00:00

URL: https://externals.io/message/4068#4068

Is possible to implement userland macros in a PHP extension or it can 
only be implemented in the core?

I know that macros is a preprocessor task and I think PHP has no 
preprocessor (does it?).

So it's possible to implement a preprocessor ?

An example (or something like that):
<?
#define DEFAULT_VALUE(check_value, default_value) (isset(check_value) ? 
check_value : default_value)

$a = DEFAULT_VALUE($my_array['some_index'], true);
?>

Best regards,

Cristiano Duarte

---

## #4069 — Wojtek Meler — 2003-08-18T19:09:56+00:00

URL: https://externals.io/message/4068#4069 · In reply to: #4068

Cristiano Duarte wrote:

> Is possible to implement userland macros in a PHP extension or it can 
> only be implemented in the core?
>
> I know that macros is a preprocessor task and I think PHP has no 
> preprocessor (does it?).
>
> So it's possible to implement a preprocessor ?

I think you can use stream wrappers with some limitations (definitions 
should be in different file than  their use).

Regards,
Wojtek

---

## #4070 — Timm Friebe — 2003-08-18T19:13:33+00:00

URL: https://externals.io/message/4068#4070 · In reply to: #4068

> Is possible to implement userland macros in a PHP extension or it can 
> only be implemented in the core?
A "userland" solution could be to use cc -E on your .php files.

> I know that macros is a preprocessor task and I think PHP has no 
> preprocessor (does it?).
It doesn't.

> So it's possible to implement a preprocessor ?
It's possible, of course, but PHP needs to compile fast as (without any 
accelerator products whatsoever) this is what happens on each request to
a script. A preprocessor takes up more time and IMHO only makes sense in
compiled languages (in the original sense of "compiled":)).

> An example (or something like that):
[...example...]

thekid@friebes:~ > cat prep.phpc 
<?
#define DEFAULT_VALUE(check_value, default_value) \
  (isset(check_value) ? check_value : default_value)

$a = DEFAULT_VALUE($my_array['some_index'], true);
?>
thekid@friebes:~ > cc -P -C -E -x c prep.phpc 
<?


$a = (isset( $my_array['some_index'] ) ?  $my_array['some_index']  :  
true ) ;
?>

Notes on command line arguments passed to cc:

-E   execute preprocessor only
-P   do not generate #<line> comments
-x c Set language to "C".
-C   leave comments intact

You might also want to do a grep -v '^$' to get rid of some of the
whitespace leftovers by the preprocessor.

- Timm

---

## #4071 — Timm Friebe — 2003-08-18T19:18:34+00:00

URL: https://externals.io/message/4068#4071 · In reply to: #4070

[...]
> Notes on command line arguments passed to cc:
> 
> -E   execute preprocessor only
> -P   do not generate #<line> comments
> -x c Set language to "C".
> -C   leave comments intact

Oops, almost forgot: You can use -imacros <filename> if you want to put
your macro definitions into a seperate file.

- Timm
