You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/extensions.md
+81-48Lines changed: 81 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,17 +81,19 @@ While the first point speaks for itself, the second may be harder to apprehend.
81
81
82
82
While some variable types have the same memory representation between C/PHP and Go, some types require more logic to be directly used. This is maybe the hardest part when it comes to writing extensions because it requires understanding internals of the Zend Engine and how variables are stored internally in PHP. This table summarizes what you need to know:
83
83
84
-
| PHP type | Go type | Direct conversion | C to Go helper | Go to C helper | Class Methods Support |
> This table is not exhaustive yet and will be completed as the FrankenPHP types API gets more complete.
@@ -102,55 +104,86 @@ If you refer to the code snippet of the previous section, you can see that helpe
102
104
103
105
#### Working with Arrays
104
106
105
-
FrankenPHP provides native support for PHP arrays through the `frankenphp.Array` type. This type represents both PHP indexed arrays (lists) and associative arrays (hashmaps) with ordered key-value pairs.
107
+
FrankenPHP provides native support for PHP arrays through `frankenphp.AssociativeArray` or direct conversion to a map or slice.
108
+
109
+
`AssociativeArray` represents a [hash map](https://en.wikipedia.org/wiki/Hash_table) composed of a `Map: map[string]any`field and an optional `Order: []string` field (unlike PHP "associative arrays", Go maps aren't ordered).
110
+
111
+
If order or association are not needed, it's also possible to directly convert to a slice `[]any` or unordered map `map[string]any`.
***Ordered key-value pairs** - Maintains insertion order like PHP arrays
141
-
***Mixed key types** - Supports both integer and string keys in the same array
142
-
***Type safety** - The `PHPKey` type ensures proper key handling
173
+
***Ordered key-value pairs** - Option to keep the order of the associative array
174
+
***Optimized for multiple cases** - Option to ditch the order for better performance or convert straight to a slice
143
175
***Automatic list detection** - When converting to PHP, automatically detects if array should be a packed list or hashmap
176
+
***Nested Arrays** - Arrays can be nested and will convert all support types automatically (`int64`,`float64`,`string`,`bool`,`nil`,`AssociativeArray`,`map[string]any`,`[]any`)
144
177
***Objects are not supported** - Currently, only scalar types and arrays can be used as values. Providing an object will result in a `null` value in the PHP array.
145
178
146
-
**Available methods:**
179
+
##### Available methods: Packed and Associative
147
180
148
-
*`SetInt(key int64, value interface{})` - Set value with integer key
149
-
*`SetString(key string, value interface{})` - Set value with string key
150
-
*`Append(value interface{})` - Add value with next available integer key
151
-
*`Len() uint32` - Get number of elements
152
-
*`At(index uint32) (PHPKey, interface{})` - Get key-value pair at index
153
-
*`frankenphp.PHPArray(arr *frankenphp.Array) unsafe.Pointer` - Convert to PHP array
181
+
*`frankenphp.PHPAssociativeArray(arr frankenphp.AssociativeArray) unsafe.Pointer` - Convert to an ordered PHP array with key-value pairs
182
+
*`frankenphp.PHPMap(arr map[string]any) unsafe.Pointer` - Convert a map to an unordered PHP array with key-value pairs
183
+
*`frankenphp.PHPPackedArray(slice []any) unsafe.Pointer` - Convert a slice to a PHP packed array with indexed values only
184
+
*`frankenphp.GoAssociativeArray(arr unsafe.Pointer, ordered bool) frankenphp.AssociativeArray` - Convert a PHP array to an ordered Go AssociativeArray (map with order)
185
+
*`frankenphp.GoMap(arr unsafe.Pointer) map[string]any` - Convert a PHP array to an unordered go map
186
+
*`frankenphp.GoPackedArray(arr unsafe.Pointer) []any` - Convert a PHP array to a go slice
0 commit comments