Structures in PHP revisited
Posted: Sat Aug 31, 2002 11:16 am
A while back, someone asked about creating structures in PHP. Well, a couple of us came up with some ideas. One of them was to use an extemely small class that held the data structure. The other was to create an array. Of course, using an array is much faster and someone even did a benchmark to show this.
All of this can be found at http://www.devnetwork.net/forums/viewtopic.php?p=8841
Now this morning when I got to work and did a little rummaging through my file system (/home/Big. And yes, I got root!), I found the little script I wrote to test some of this and realized something. You could almost totally mimic the use of structures in php with out the penalty or overhead of a class by doing the below.
Now the only thing that needs to be done is create something like a pointer to a structure. But at least this way, the structue created is actually an array that is not internal to a class, so there are no speed or performance considerations.
However, just like structures, this acts as a template so that whenever another one is needed, you just run the create_Struct() method as above.
Now you could so something like this to act like a pointer.
So instead of
as would be done in C, do
So, you still can't create structures in PHP, but you can mimic the functionality. And there's tons of power in sturctures.
The factory class could also be a function too.
If any of you C guys out there see any issues with this, pipe up.
Later on,
BDKR
All of this can be found at http://www.devnetwork.net/forums/viewtopic.php?p=8841
Now this morning when I got to work and did a little rummaging through my file system (/home/Big. And yes, I got root!), I found the little script I wrote to test some of this and realized something. You could almost totally mimic the use of structures in php with out the penalty or overhead of a class by doing the below.
Code: Select all
<?php
# A factory class for a structure
class Struct
{
# This is what will create the structure
function create_Struct($struct_array)
{
$struct_array=array(
age => 0,
name => "",
initials => "",
sex => ""
);
return $struct_array;
}
}
# Create the factory class
$my_struct = new Struct;
# Create one structure/array
$yam = $my_struct->create_Struct($yam);
# Create another structure/array
$blam = $my_struct->create_Struct($blam);
# Test the creation of these structure/arrays
print_r($yam);
print_r($blam);
?>However, just like structures, this acts as a template so that whenever another one is needed, you just run the create_Struct() method as above.
Now you could so something like this to act like a pointer.
Code: Select all
# This will work as a pointer.
$pointer = &$yam;
print_r($pointer);Code: Select all
printf("%s", pointer->sex)Code: Select all
echo(pointerїsex]);The factory class could also be a function too.
If any of you C guys out there see any issues with this, pipe up.
Later on,
BDKR