blob: 315286e1565985bf9c5098a713691dc91e83f2c0 [file] [log] [blame] [view]
Paul Yange0e54662016-09-15 11:09:01 -07001This directory contains the Protocol Buffers runtime implementation via both a
2pure PHP package and a native c extension. The pure PHP package is intended to
3provide usability to wider range of PHP platforms, while the c extension is
4intended to provide higher performance. Both implementations provide the same
5runtime APIs and share the same generated code. Users dont need to re-generate
6code for the same proto definition when they want to switch the implementation
7later.
8
9Both implementations make use of generated PHP code that defines message and
10enum types in PHP. We strongly recommend using protoc's PHP generation support
11with .proto files. The build process in this directory only installs the
12extension/package; you need to install protoc as well to have PHP code
13generation functionality.
14
15## Requirements
16
Protobuf Team Botab678472023-05-15 08:12:56 -070017Using the PHP runtime library requires:
Paul Yange0e54662016-09-15 11:09:01 -070018
Protobuf Team Botab678472023-05-15 08:12:56 -070019- C extension: PHP 7.x, 8.x
20- [PHP package](http://php.net/downloads.php): PHP 7.4+
21
22For information on how the support levels for PHP versions will change over
23time, see
24[Supported PHP versions](https://cloud.google.com/php/getting-started/supported-php-versions).
Paul Yange0e54662016-09-15 11:09:01 -070025
26## Installation
27
28### C Extension
29
30#### Prerequirements
31
32To install the c extension, the following tools are needed:
Paul Yange0e54662016-09-15 11:09:01 -070033* libtool
34* make
35* gcc
36* pear
37* pecl
38
39On Ubuntu, you can install them with:
40```
Mike Kruskaled5c57a2022-08-10 22:51:29 -070041sudo apt-get install -y php-pear php-dev libtool make gcc
Paul Yange0e54662016-09-15 11:09:01 -070042```
43On other platforms, please use the corresponding package managing tool to
44install them before proceeding.
45
46#### Installation from Source (Building extension)
47
48To build the c extension, run the following command:
49```
50cd ext/google/protobuf
51pear package
52sudo pecl install protobuf-{VERSION}.tgz
53```
54
55#### Installation from PECL
56
57When we release a version of Protocol Buffers, we will upload the extension to
58[PECL](https://pecl.php.net/). To use this pre-packaged extension, simply
59install it as you would any other extension:
60
61```
62sudo pecl install protobuf-{VERSION}
63```
64
65### PHP Package
66
67#### Installation from composer
68
69Simply add "google/protobuf" to the 'require' section of composer.json in your
70project.
71
deannagarcia0cb3c442022-09-01 13:50:26 -040072To use the pure PHP implementation, you need to install bcmath.
73
Paul Yange0e54662016-09-15 11:09:01 -070074### Protoc
75
76Once the extension or package is installed, if you wish to generate PHP code
77from a `.proto` file, you will also want to install the Protocol Buffers
78compiler (protoc), as described in this repository's main `README` file. The
79version of `protoc` included in the latest release supports the `--php_out`
80option to generate PHP code:
81```
82protoc --php_out=out_dir test.proto
83```
84
85## Usage
86
Paul Yange0e54662016-09-15 11:09:01 -070087For generated code:
88 https://developers.google.com/protocol-buffers/docs/reference/php-generated
89
90Known Issues
91------------
92
93* Missing native support for well known types.
94* Missing support for proto2.
95* No API provided for clear/copy messages.
96* No API provided for encoding/decoding with stream.
97* Map fields may not be garbage-collected if there is cycle reference.
98* No debug information for messages in c extension.
99* HHVM not tested.
Bo Yangb1559582016-09-23 22:22:18 +0000100* C extension not tested on windows, mac, php 7.0.
Paul Yange0e54662016-09-15 11:09:01 -0700101* Message name cannot be Empty.
Paul Yang66bae582019-09-26 13:04:07 -0700102
103## Development
104
Paul Yang66bae582019-09-26 13:04:07 -0700105### Test Native PHP
106
107```
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700108# Install Dependencies (Linux)
109apt-get install bazel composer php-dev
110
Paul Yang66bae582019-09-26 13:04:07 -0700111# Download protobuf
112git clone https://github.com/protocolbuffers/protobuf.git
113cd protobuf
114
115# Build protoc
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700116bazel build :protoc
Paul Yang66bae582019-09-26 13:04:07 -0700117
118# Test native php
119cd php
120composer install
121composer test
122```
123
Nikhil Pothuru11a5b032019-09-29 19:49:17 -0700124### Test C Extension
Paul Yang66bae582019-09-26 13:04:07 -0700125
Nikhil Pothuru11a5b032019-09-29 19:49:17 -0700126After you have finished testing the native php, you can test the c extension:
Paul Yang66bae582019-09-26 13:04:07 -0700127```
128cd tests
129./test.sh 5.6 # The php runtime version.
130 # We provide 5.5, 5.5-zts, 5.6, 5.6-zts, 7.0, 7.0-zts, 7.1, 7.1-zts, 7.2, 7.2-zts, 7.3 and 7.3-zts
131 # ls /usr/local for more details
132```
133
134If you want to use gdb to debug the c extension, you can do:
135```
136./gdb_test.sh
137```