PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Sebastian Potasiak   Array to Object converter   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example of using class
Class: Array to Object converter
Turn any array into an object
Author: By
Last change: Can't delete account, so am deleting code.
Date: 9 years ago
Size: 2,014 bytes
 

Contents

Class file image Download
<?php
     define
(PATH, $_PHP_SELF);
     include(
'toobj.php'); // Including class' file
    
    
$arrNum = array('first value', 'second value', 3, 4); // New numeric array
    
$objNum = new toObj($arrNum); // Transporting a numeric array to object

     // Listing numeric by loop
    
echo '<b>Values:</b><br />';
     for (
$i = 0; $i < $objNum->varCount(); $i++)
     {
          echo
$i . ': <i>' . $objNum->$i . '</i><br />';
     }
    
// Showing each value
    
echo 'Each: <i>' . $objNum->{1} . '</i><br /><br />'; // will show: second value
    
     // New assoc array
    
$arrAssoc = array(
         
'me' => 'Sebastian',
         
'you' => 'Anonymous',
         
'he' => 'Bill'
    
);
    
$objAssoc = new toObj($arrAssoc); // Transporting an assoc array to object
    
     // Listing assoc by loop
    
echo '<b>Values:</b><br />';
     foreach (
$objAssoc as $key => $value)
     {
          echo
$key . ': <i>' . $value . '</i><br />';
     }
    
// Showing each value
    
echo 'Each: <i>' . $objAssoc->you . '</i><br /><br />'; // will show: Anonymous
    
     // New multivariate array
    
$arrMulti = array(
         
'me' => 'Sebastian',
         
'car' => array('mercedes', 'lexus'),
         
'motorbike' => array(
              
'old' => 'yamaha',
              
'new' => 'suzuki'
         
)
     );
    
$objMulti = new toObj($arrMulti);
    
    
// Listing multivariate by recursive function
    
function listMulti($object)
     {
          foreach (
$object as $key => $value)
          {
               if (
is_a($object->$key, 'toObj')) listMulti($object->$key);
               else echo
$key . ': <i>' . $value . '</i><br />';
          }
     }
     echo
'<b>Values:</b><br />';
    
listMulti($objMulti);
    
// Showing each value
    
echo 'Each 1: <i>' . $objMulti->motorbike->old . '</i><br />'; // will show: yamaha
    
echo 'Each 2: <i>' . $objMulti->car->{1} . '</i><br /><br />'; // will show: lexus
?>