Page 1 of 1

Structures in PHP revisited

Posted: Sat Aug 31, 2002 11:16 am
by BDKR
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.

Code: Select all

<?php
# A factory class for a structure
class Struct
  &#123;
  # This is what will create the structure
  function create_Struct($struct_array)
	&#123;
	$struct_array=array(
	  age => 0,
	  name => "",
	  initials => "",
	  sex => ""
	  ); 
	return $struct_array;
	&#125;
  &#125;
  
# 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);
?>
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.

Code: Select all

# This will work as a pointer.
$pointer = &$yam;
print_r($pointer);
So instead of

Code: Select all

printf("%s", pointer->sex)
as would be done in C, do

Code: Select all

echo(pointer&#1111;sex]);
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

sweet

Posted: Sat Aug 31, 2002 12:10 pm
by AVATAr
Seet Stuff...
:D

Posted: Sat Aug 31, 2002 12:56 pm
by hob_goblin
whats the point? i mean it just seems like you're making an array with some preset values

Posted: Sat Aug 31, 2002 1:25 pm
by BDKR
hob_goblin wrote: whats the point? i mean it just seems like you're making an array with some preset values
That's exactly what a structure is and is all about. 8O

From K&R (Brian W Kernighan)
The main use of structures is to lump together collections of
disparate variable types, so they can conveniently be treated as a
unit. For example, if we were writing a compiler or assembler,
we might need for each identifier information like its name (a
character array), its source line number (an integer), some type
information (a character, perhaps), and probably a usage count
(another integer).
In C, this is a big deal and is used extensivley. Google is your friend. Look up the use of structures there. Or just look at the code for the Linux kernel and tell me if you see objects or structures.

Better yet, here is an example. I recently wrote a load balancer in php. What is being load balanced is a pool of replicated database machines. Now the code is largely procedural, but the connections to the db's are managed using objects.

It works fine for the most part.

However, there is a significant performance penalty in the use of objects as opposed to arrays, which is what class Struct is really creating. Furthermore, a class definition and a structure definition BOTH behave as templates correct? The difference here is that we offload the methods in the classes to just plain functions but maintain the encapsulated data. Perhaps the thinking is more convuluted, but the performance gain will speak for itself. Just ask John Carmack. The quake engines are not using objects to represent monsters, weapons, players, etc...... they're using structures! Well, at least the q and qII engines are.

Keep progging and you'll see.

Later on,
BDKR

Posted: Sat Aug 31, 2002 11:59 pm
by phpPete
Even though it's a small class, why even bother writing the class, since it's just a wrapper for the function create_struct().

Posted: Sun Sep 01, 2002 4:32 pm
by BDKR
That's a good point and it's something that I realize by the end of the post. If you check the second to last line, you'll see that I said that I can be a function also.

Later on,
BDKR

Posted: Sun Sep 01, 2002 9:49 pm
by llimllib
/me applauds. I think that's the prettiest solution to an ugly problem that I've seen - you should write up a small article about it.