Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 1 | This directory contains the Protocol Buffers runtime implementation via both a |
| 2 | pure PHP package and a native c extension. The pure PHP package is intended to |
| 3 | provide usability to wider range of PHP platforms, while the c extension is |
| 4 | intended to provide higher performance. Both implementations provide the same |
| 5 | runtime APIs and share the same generated code. Users don’t need to re-generate |
| 6 | code for the same proto definition when they want to switch the implementation |
| 7 | later. |
| 8 | |
| 9 | Both implementations make use of generated PHP code that defines message and |
| 10 | enum types in PHP. We strongly recommend using protoc's PHP generation support |
| 11 | with .proto files. The build process in this directory only installs the |
| 12 | extension/package; you need to install protoc as well to have PHP code |
| 13 | generation functionality. |
| 14 | |
| 15 | ## Requirements |
| 16 | |
Protobuf Team Bot | ab67847 | 2023-05-15 08:12:56 -0700 | [diff] [blame] | 17 | Using the PHP runtime library requires: |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 18 | |
Protobuf Team Bot | ab67847 | 2023-05-15 08:12:56 -0700 | [diff] [blame] | 19 | - C extension: PHP 7.x, 8.x |
| 20 | - [PHP package](http://php.net/downloads.php): PHP 7.4+ |
| 21 | |
| 22 | For information on how the support levels for PHP versions will change over |
| 23 | time, see |
| 24 | [Supported PHP versions](https://cloud.google.com/php/getting-started/supported-php-versions). |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 25 | |
| 26 | ## Installation |
| 27 | |
| 28 | ### C Extension |
| 29 | |
| 30 | #### Prerequirements |
| 31 | |
| 32 | To install the c extension, the following tools are needed: |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 33 | * libtool |
| 34 | * make |
| 35 | * gcc |
| 36 | * pear |
| 37 | * pecl |
| 38 | |
| 39 | On Ubuntu, you can install them with: |
| 40 | ``` |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 41 | sudo apt-get install -y php-pear php-dev libtool make gcc |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 42 | ``` |
| 43 | On other platforms, please use the corresponding package managing tool to |
| 44 | install them before proceeding. |
| 45 | |
| 46 | #### Installation from Source (Building extension) |
| 47 | |
| 48 | To build the c extension, run the following command: |
| 49 | ``` |
| 50 | cd ext/google/protobuf |
| 51 | pear package |
| 52 | sudo pecl install protobuf-{VERSION}.tgz |
| 53 | ``` |
| 54 | |
| 55 | #### Installation from PECL |
| 56 | |
| 57 | When 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 |
| 59 | install it as you would any other extension: |
| 60 | |
| 61 | ``` |
| 62 | sudo pecl install protobuf-{VERSION} |
| 63 | ``` |
| 64 | |
| 65 | ### PHP Package |
| 66 | |
| 67 | #### Installation from composer |
| 68 | |
| 69 | Simply add "google/protobuf" to the 'require' section of composer.json in your |
| 70 | project. |
| 71 | |
deannagarcia | 0cb3c44 | 2022-09-01 13:50:26 -0400 | [diff] [blame] | 72 | To use the pure PHP implementation, you need to install bcmath. |
| 73 | |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 74 | ### Protoc |
| 75 | |
| 76 | Once the extension or package is installed, if you wish to generate PHP code |
| 77 | from a `.proto` file, you will also want to install the Protocol Buffers |
| 78 | compiler (protoc), as described in this repository's main `README` file. The |
| 79 | version of `protoc` included in the latest release supports the `--php_out` |
| 80 | option to generate PHP code: |
| 81 | ``` |
| 82 | protoc --php_out=out_dir test.proto |
| 83 | ``` |
| 84 | |
| 85 | ## Usage |
| 86 | |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 87 | For generated code: |
| 88 | https://developers.google.com/protocol-buffers/docs/reference/php-generated |
| 89 | |
| 90 | Known 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 Yang | b155958 | 2016-09-23 22:22:18 +0000 | [diff] [blame] | 100 | * C extension not tested on windows, mac, php 7.0. |
Paul Yang | e0e5466 | 2016-09-15 11:09:01 -0700 | [diff] [blame] | 101 | * Message name cannot be Empty. |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 102 | |
| 103 | ## Development |
| 104 | |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 105 | ### Test Native PHP |
| 106 | |
| 107 | ``` |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 108 | # Install Dependencies (Linux) |
| 109 | apt-get install bazel composer php-dev |
| 110 | |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 111 | # Download protobuf |
| 112 | git clone https://github.com/protocolbuffers/protobuf.git |
| 113 | cd protobuf |
| 114 | |
| 115 | # Build protoc |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 116 | bazel build :protoc |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 117 | |
| 118 | # Test native php |
| 119 | cd php |
| 120 | composer install |
| 121 | composer test |
| 122 | ``` |
| 123 | |
Nikhil Pothuru | 11a5b03 | 2019-09-29 19:49:17 -0700 | [diff] [blame] | 124 | ### Test C Extension |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 125 | |
Nikhil Pothuru | 11a5b03 | 2019-09-29 19:49:17 -0700 | [diff] [blame] | 126 | After you have finished testing the native php, you can test the c extension: |
Paul Yang | 66bae58 | 2019-09-26 13:04:07 -0700 | [diff] [blame] | 127 | ``` |
| 128 | cd 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 | |
| 134 | If you want to use gdb to debug the c extension, you can do: |
| 135 | ``` |
| 136 | ./gdb_test.sh |
| 137 | ``` |