# How does the interpreter work

- Thread: https://externals.io/message/46478
- Original archive: https://news-web.php.net/php.internals/46478
- Started by Christian Grobmeier on 2009-12-21T13:56:08+00:00
- Messages: 8
- 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

---

## #46478 — Christian Grobmeier — 2009-12-21T13:56:08+00:00

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

Hi all,

I would like to learn more about how the interpreter works, but I was
unable to find good documents on the web. Basically I am thinking on
something about allocation of variables, how does object creation work
and such stuff. Maybe something on the overall architecture of PHP
would be of interest too.

In java world there is the JVM specification, I hoped there is
something for PHP too.

Best regards and thanks for all help,
Christian

---

## #46480 — Rob Nicholson — 2009-12-21T14:27:02+00:00

URL: https://externals.io/message/46478#46480 · In reply to: #46478

Christian Grobmeier <grobmeier@gmail.com> wrote on 21/12/2009 13:56:08:


> I would like to learn more about how the interpreter works, but I was
> unable to find good documents on the web. Basically I am thinking on
> something about allocation of variables, how does object creation work
> and such stuff. Maybe something on the overall architecture of PHP
> would be of interest too.
>
> In java world there is the JVM specification, I hoped there is
> something for PHP too.

Hi Christian,

The PHP architecture is a little different from the JVM in that it does not
explicitly document/specify the interface between the compiler and the
bytecode/opcode interpreter the way that Java does. It still exists though.

I suggest you look at the links under here:

http://www.php.net/manual/en/internals2.php
In particular:
 http://www.php.net/manual/en/internals2.opcodes.php

Another good reference is Sara Goleman's book "Extending and Embedding PHP"

Andy Wharmby produced a set of charts which you can find on Zoe's Blog
here : http://zoomsplatter.blogspot.com/2008/08/php-opcodes.html .
These may help you to make a fast start  understanding the overall design.

Rob.

---

## #46481 — mm w — 2009-12-21T16:02:12+00:00

URL: https://externals.io/message/46478#46481 · In reply to: #46480

Hello,

this document is clearly outdated and for me so far confuse just gave
a try for 5 mins, and I am worry about the printf tech, lucky you are
not to be in multi-thread env..., at least fprintf on the stderr to
read un-buffered results, those guys have a job? maybe greenhouse
keepers certainly not developers.


Christian, the best way to enter in the zend engine, it's to create a
zend extension project, then go thru all the step of the code to
manipulate the hashtable, learn where object are living in the tree,
then when you know how to play with the "public" interface, that's
really easy to go deeper, the zend "bytecode engine" is so far less
complex that what you could find in any JVM (I know a couple of things
in area) even python bytecode and how the interpreter is able to
handle various version,

in two weeks you should have done your tour by using the zend
interface and reading the code, php zend core is tiny in term of code,
the only things that could appear weird and clumsy is the semantic
code used, this is the only thing that could appear like a mind
barrier.

Best,


>
> Christian Grobmeier <grobmeier@gmail.com> wrote on 21/12/2009 13:56:08:
>
>
> > […]
>
> Hi Christian,
>
> The PHP architecture is a little different from the JVM in that it does not
> explicitly document/specify the interface between the compiler and the
> bytecode/opcode interpreter the way that Java does. It still exists though.
>
> I suggest you look at the links under here:
>
> http://www.php.net/manual/en/internals2.php
> In particular:
>  http://www.php.net/manual/en/internals2.opcodes.php
>
> Another good reference is Sara Goleman's book "Extending and Embedding PHP"
>
> Andy Wharmby produced a set of charts which you can find on Zoe's Blog
> here : http://zoomsplatter.blogspot.com/2008/08/php-opcodes.html .
> These may help you to make a fast start  understanding the overall design.
>
> Rob.

---

## #46482 — johannes@php.net — 2009-12-21T16:33:51+00:00

URL: https://externals.io/message/46478#46482 · In reply to: #46478

Hi,


> I would like to learn more about how the interpreter works, but I was
> unable to find good documents on the web. Basically I am thinking on
> something about allocation of variables, how does object creation work
> and such stuff. Maybe something on the overall architecture of PHP
> would be of interest too.
> 
> In java world there is the JVM specification, I hoped there is
> something for PHP too.

Well there's one quite complete document about it available - the source
code ;-) PHP isn't developed following standards but simply by
implementing and extending it. PHP is defined by the implementation ...

But: What exactly do you want to learn? Do you want to learn how to use
it better or "academic reasons" or something else? - Depending on all
that the focus of the learning might be quite different.

As a starting point for this I recently created a small toy project: A
minimal basic inspired language on top of the ZendVM, just 200 lines of
C code which might give a few pointers to do further research. It's
quite hackish ugly code but enough to compile and execute a script like
this:

10 GOTO 40
20 PRINT "B"
30 END
40 PRINT "A"
50 GOTO 20

This all will be compiled to Zend Opcodes which can be executed, cached
with APC, dumped by vld, ...

I hope I find the time to write the documentation around it, other than
that this code is absolutely useless :-)

http://github.com/johannes/pasic

johannes

---

## #46483 — mm w — 2009-12-21T16:39:53+00:00

URL: https://externals.io/message/46478#46483 · In reply to: #46482

Pas mieux, and I am sure johannes did not find the need to express
himself and/or these mind  limitations on his blog :)

harf I am so bad I know.

-mmw

2009/12/21 Johannes Schlüter <johannes@php.net>:
> Hi,
>
> 
> > […]
>
> Well there's one quite complete document about it available - the source
> code ;-) PHP isn't developed following standards but simply by
> implementing and extending it. PHP is defined by the implementation ...
>
> But: What exactly do you want to learn? Do you want to learn how to use
> it better or "academic reasons" or something else? - Depending on all
> that the focus of the learning might be quite different.
>
> As a starting point for this I recently created a small toy project: A
> minimal basic inspired language on top of the ZendVM, just 200 lines of
> C code which might give a few pointers to do further research. It's
> quite hackish ugly code but enough to compile and execute a script like
> this:
>
> 10 GOTO 40
> 20 PRINT "B"
> 30 END
> 40 PRINT "A"
> 50 GOTO 20
>
> This all will be compiled to Zend Opcodes which can be executed, cached
> with APC, dumped by vld, ...
>
> I hope I find the time to write the documentation around it, other than
> that this code is absolutely useless :-)
>
> http://github.com/johannes/pasic
>
> johannes

---

## #46486 — Christian Grobmeier — 2009-12-21T17:41:27+00:00

URL: https://externals.io/message/46478#46486

Hi all again,

first thanks, for all the links.

> > […]
>
> Well there's one quite complete document about it available - the source
> code ;-) PHP isn't developed following standards but simply by
> implementing and extending it. PHP is defined by the implementation ...

I was afraid it is :-)

> But: What exactly do you want to learn? Do you want to learn how to use
> it better or "academic reasons" or something else? - Depending on all
> that the focus of the learning might be quite different.

I don't want to start with PHP development itself. I just want to be
able to learn how to use the language best. For example, I can tell
you quite good when to use what in java world, because I know how an
object is being created. This helps me with writing performant code
and with debugging. I want to getthat ability with PHP too.

> This all will be compiled to Zend Opcodes which can be executed, cached
> with APC, dumped by vld, ...
> I hope I find the time to write the documentation around it, other than
> that this code is absolutely useless :-)
>
> http://github.com/johannes/pasic

Thanks for sharing this. However, I am not a skilled C developer nor
do I know exactly what Opcodes are or even APC or vld.
I really hoped that there is some kind of specification around. I also
took a look at:
http://www.php.net/manual/en/internals2.php

but it helped me not too much. Its more about writing extensions to
PHP. But I would like to understand how the object oriented features
really work. I am afraid with my basic knowledge on C I have to dig
very deep  and need to compile everything myself on my Mac, which
already gave me some headache.

Thanks
Christian

>
> johannes

---

## #46484 — Christian Grobmeier — 2009-12-22T06:50:42+00:00

URL: https://externals.io/message/46478#46484 · In reply to: #46480

Hi,

> Rob Nicholson wrote:
> Another good reference is Sara Goleman's book "Extending and Embedding PHP"

actually this is what i was searching for. I will order this book now.

Thanks for the tipp!
Christian

---

## #46492 — Richard Lynch — 2009-12-22T15:16:43+00:00

URL: https://externals.io/message/46478#46492 · In reply to: #46486

> > […]
>
> I don't want to start with PHP development itself. I just want to be
> able to learn how to use the language best. For example, I can tell
> you quite good when to use what in java world, because I know how an
> object is being created. This helps me with writing performant code
> and with debugging. I want to getthat ability with PHP too.

I really don't think you need to understand PHP internals at the same
level as Java to get performant code.

Most of your performance issues are either A) obvious (long loops with
expensive bodies), or B) outside of PHP (i.e., the DB, network calls,
disk access).

The core language of PHP is very minimalist.

-- 
Some people ask for gifts here.
I just want you to buy an Indie CD for yourself:
http://cdbaby.com/search/from/lynch
