[KLUG Programming] PHP arrays vs. structures

Robert G. Brown bob at whizdomsoft.com
Mon Jul 19 19:44:35 EDT 2004


On Mon, 19 Jul 2004 12:50:54 -0400, Adam Tauno Williams wrote:

>But other languages (C, etc...) as well as documents (XML, etc...) do
>clearly distinguish between an array and a structure.
Yes, and these are (in general) langauges that have much stronger roles 
for data types and distinct data structures than PHP has exhibited his-
torically.

A C structure is a very different entity (at the lowest level) than a 
C array. I'm not at all convinced that this is the case for PHP (I have
not looked at PHP 5  yet, so my commments do not address this. However, since
PHP has been around for years, and PHP5 has been around for some days (in
production form), this is most probably not obsolete information.

PHP and PERL (a principle antecedent of PHP) are languages with VERY WEAK
data typing. Such languages tend to have a small number of really distinct
data structures, and the implementors have used those to simulate others.

Using the numeric
>key = array hack I've constructed...
Yes, not ethe extensive use of metadata in the code, and functions to access
those. These ideas have been around for decades, and while there's nothing
wrong with them, they indicate that level of abstraction the implementors are 
working on. 

Note especially the rols of the functions (primitives, or user-defined?) like 
<traceMessage> and <gettype>, then consider what XML_RPC_value (or ANY such 
entity passed to <new>. Finally, note the recursive nature of this function,
which is natural to any language which employs nested arrays or other essen-
tially recursive data structures.

>    function wrap($value) {
>      $this->traceMessage('@OGoDocument:wrap');
>      switch(gettype($value)) {
>        case 'boolean':
>          $this->traceMessage('@OGoDocument:wrap.boolean');
>          $value = new XML_RPC_value($value, 'boolean');
>         break;
>        case 'integer':
>          $this->traceMessage('@OGoDocument:wrap.integer');
>          $value = new XML_RPC_value($value, 'int');
>         break;
>        case 'double':
>          $this->traceMessage('@OGoDocument:wrap.double');
>          $value = new XML_RPC_value($value, 'double');
>         break;
>        case 'string':
>          $this->traceMessage('@OGoDocument:wrap.string');
>          $value = new XML_RPC_value($value, 'string');
>         break;
>        case 'array':
>          $this->traceMessage('@OGoDocument:wrap.array');
>          $a = array();
>          reset($value);
>          if (key($value) == '0') {
>            //array
>            $this->traceMessage('@OGoDocument:wrap.array.array');
>            $a_type = 'array';
>            foreach ($value as $v)
>               array_push($a, $this->wrap($v));
>           } else {
>               //struct
>               $this->traceMessage('@OGoDocument:wrap.struct'); 
>               $a_type = 'struct';
>               foreach ($value as $k => $v)
>                 $a[$k] = $this->wrap($v);
>             }
>          $value = new XML_RPC_value($a, $a_type);
>         break;
>        case 'object':
>          $this->traceMessage('@OGoDocument:wrap.object');
>          /// NOT SUPPORTED
>         break;
>        case 'resource':
>          $this->traceMessage('@OGoDocument:wrap.resource');
>          /// NOT SUPPORTED
>         break;
>        case 'NULL':
>          $this->traceMessage('@OGoDocument:wrap.NULL');
>          /// NOT SUPPORTED
>         break;
>       }
>      return $value;
>     }
>
>... which seems to be able to turn PHP structures into documents that
>can be transported to a web service.
I don't see why not. Are you attempting such?

							Regards,
							---> RGB <---



More information about the Programming mailing list