Can anyone give me example for structs data type in php ? How come there is something like structs in php all of a sudden ?
A structure is a collection of one or more variables, possibly of different types, grouped under a single name. It is a user-defined data type. They help to organize complicated data in large programs, as they allow a group of logically related variables to be treated as one.
The answer is yes unless you are using an obsolete compiler that does not support initialization of structures with string class members. Make sure that the structure definition has access to the std namespace. You can do this by moving the using directive so that it is above the structure definition.
A struct is also a collection of data items, except with a struct the data items can have different data types, and the individual fields within the struct are accessed by name instead of an integer index.
Closest you'd get to a struct is an object with all members public.
class MyStruct { public $foo; public $bar; } $obj = new MyStruct(); $obj->foo = 'Hello'; $obj->bar = 'World';
I'd say looking at the PHP Class Documentation would be worth it. If you need a one-off struct, use the StdObject as mentioned in alex's answer.
You can use an array
$something = array( 'key' => 'value', 'key2' => 'value2' );
or with standard object.
$something = new StdClass(); $something->key = 'value'; $something->key2 = 'value2';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With