Structures in PHP revisited

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Structures in PHP revisited

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

sweet

Post by AVATAr »

Seet Stuff...
:D
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

whats the point? i mean it just seems like you're making an array with some preset values
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post 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().
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply