On Mo, 2019-03-04 at 07:30 -0800, Steven Penny wrote:
> On Mon, 04 Mar 2019 02:23:46, Peter Kokot wrote:
> > 
> > Now, interesting is that in bash and some langs (where the main
> > environment is CLI), there is by default newline echoed. In PHP and
> > other languages there isn't. Changing default functionality of echo
> > in
> > PHP is like changing left-hand traffic countries to use right-hand
> > traffic. A new function name would be needed for that. You can
> > start
> > with creating an extension for this.
> I think the best option is a new function like "puts" or
> "posix_puts". I looked
> into this but im having trouble. i started with "var_dump" because
> that does
> produce a newline by default [1]:
> 
>     php_printf("%sbool(false)\n", COMMON);
> 
> new function would be most similar to "print", so i went to look at
> that code,
> but it seems "print" uses "echo" internally [2]:
> 
>     opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);


What you are seeing is that "echo" and "print" are no functions, but
language constructs (this is why the follow different rules, i.e. no
need for parenthesis around parameters etc.) which makes them a bit
special.

Second thing you are seeing is PHP's modular architecture. The engine
is it's own independent thing and you could (theoretically, not sure if
that is fully kept) compile the engine stand-alone without "PHP" this
is why zend_write is a hook, which is registered from PHP on startup.
Second thing is that PHP's output doesn't go to the output directly,
but goes to the SAPI layer into a server-environment specific API.

Adding a new language construct is relatively complex as you have to
extend the parser and the PHP syntax etc. The way I (spontaneously,
without much thinking, so maybe bad) would do this by extending th
ZEND_ECHO opcode (= bytecode command for the Zend virtual machine) with
an operand which ells whether new line is requested or not and then in
the compiler produce the according code. (see parser files and
zend_compile for how echo/print are compiled)

The flow there is PHP code is scanned&parsed into an AST, that AST is
then compiled into opcode and that opcode is then executed on the VM
(ad probably stored n opcode cache)

Also mind that zend_vm_def.h isn't directly used while compiling the
engine, but has to be processed using zend_vm_gen.php to generate the
actual VM out of it.

Oh and as another thought for implementation: Eventually this can be
done in the parser already. Having
   echoln "foo";
pretend to be parsed as 
   echo "foo", PHP_EOL;
This could eventually be a change by touching the parser only. (but
again - I didn't do any research on this, see zend_language_parser.y
for the parsing rules for echo)


Massively simpler is to create new thing as a function. Similar to
printf(). For that you need a PHP_FUNCTION for the implementation and
then an function entry using PHP_FE in some function table.
See ext/standard/formatted_print.c for the implementation of printf()
and friends and ext/standard/basic_functions.c for the function table.
This can also be done in a custom extension see http://www.phpinternals
book.com/php7/extensions_design/php_functions.html

johannes

P.S. I see this more as a learning exercise than as a project which
will be added to PHP

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to