Re: [PHP] bless function: a better aproach

2004-10-28 Thread raditha dissanayake
Curt Zirzow wrote:
* Thus wrote Francisco M. Marzoa Alonso:
 

Giving it a round, this seems to be a better aproach than the previous 
one. It has the advantage of provide direct access to the original array 
obtained from casting without boring about ___FAKE_KEYS_.

?
...
$Obj = new TestClass ();
//$Clone = (array) $Obj;
$Clone = obj2array ( $Obj );
   

I still dont see why a simple:
 $obj = new TestClass();
 $data = serialize($obj);
 $Clone = unserialize($data);
Cant doesn't work for you.
 

ah so that what the OP is trying to do!

--
Raditha Dissanayake.

http://www.radinks.com/sftp/ | http://www.raditha.com/megaupload
Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader
Graphical User Inteface. Just 128 KB | with progress bar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] bless function

2004-10-27 Thread Francisco M. Marzoa Alonso
Ok, I think I've got it now. The code is autoexplicative, the only 
restriction is that the object must be converted to an array using the 
function obj2array instead of a direct cast. There's another ways to do 
this without the needless of that obj2array function, but I think this 
is not really a limitation.

Here it is:
?
function obj2array ( $Instance ) {
   $clone = (array) $Instance;
   $rtn = $clone;
   while ( list ($key, $value) = each ($clone) ) {
   $aux = explode (\0, $key);
   $newkey = $aux[count($aux)-1];
   if ( $newkey != $key ) {
   $rtn[$newkey] = $rtn[$key];
   $rtn['___FAKE_KEYS_'][] = $newkey;
   }
   }
   return $rtn;
}

function bless ( $Instance, $Class ) {
   if ( ! (is_array ($Instance) ) ) {
   return NULL;
   }
   // First drop faked keys if available
   foreach ( $Instance['___FAKE_KEYS_'] as $fake_key ) {
   unset ($Instance[$fake_key]);
   }
   unset ( $Instance['___FAKE_KEYS_'] );
   // Get serialization data from array
   $serdata = serialize ( $Instance );
   /* For an array serialized data seems to meant:
array_tag:array_count:{array_elems}
   array_tag is always 'a'
   array_count is the number of elements in the array
   array_elems are the elemens in the array
 For an object seems to meant:
 
object_tag:object_class_name_len:object_class_name:object_count:{object_members}

   object_tag is always 'O'
   object_class_name_len is the length in chars of 
object_class_name string
   object_class_name is a string with the name of the class
   object_count is the number of object members
   object_members is the object_members itself (exactly equal to 
array_elems)
   */

   list ($array_params, $array_elems) = explode ('{', $serdata, 2);
   list ($array_tag, $array_count) = explode (':', $array_params, 3 );
   $serdata = O:.strlen 
($Class).:\$Class\:$array_count:{.$array_elems;

   $Instance = unserialize ( $serdata );
   return $Instance;
}
class TestClass {
   private $One=1;
   protected $Two=2;
   public $Three=3;
   public function sum() {
   return $this-One+$this-Two+$this-Three;
   }
}
$Obj = new TestClass ();
//$Clone = (array) $Obj;
$Clone = obj2array ( $Obj );
echo As the original object:br;
print_r ($Obj);
echo brbrAs an array:br;
print_r ($Clone);
$Clone[One]=7;
$Clone[Two]=7;
$Clone[Three]=7;
bless ( $Clone, TestClass );
echo brbrAfter blessing as a TestClass instance:br;
print_r ($Clone);
echo brbrCalling sum method: ;
echo $Clone-sum();
echo brThe array was blessed! miracle!!! ;-)br;
?
Hope someone appart from me find it useful. :-)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] bless function: a better aproach

2004-10-27 Thread Francisco M. Marzoa Alonso
Giving it a round, this seems to be a better aproach than the previous 
one. It has the advantage of provide direct access to the original array 
obtained from casting without boring about ___FAKE_KEYS_.

?
function obj2array ( $Instance ) {
   $clone = (array) $Instance;
   $rtn = array ();
   $rtn['___SOURCE_KEYS_'] = $clone;
   while ( list ($key, $value) = each ($clone) ) {
   $aux = explode (\0, $key);
   $newkey = $aux[count($aux)-1];
   $rtn[$newkey] = $rtn['___SOURCE_KEYS_'][$key];
   }
   return $rtn;
}
function bless ( $Instance, $Class ) {
   if ( ! (is_array ($Instance) ) ) {
   return NULL;
   }
   // First get source keys if available
   if ( isset ($Instance['___SOURCE_KEYS_'])) {
   $Instance = $Instance['___SOURCE_KEYS_'];
   }
   // Get serialization data from array
   $serdata = serialize ( $Instance );
   /* For an array serialized data seems to meant:
array_tag:array_count:{array_elems}
   array_tag is always 'a'
   array_count is the number of elements in the array
   array_elems are the elemens in the array
 For an object seems to meant:
 
object_tag:object_class_name_len:object_class_name:object_count:{object_members}

   object_tag is always 'O'
   object_class_name_len is the length in chars of 
object_class_name string
   object_class_name is a string with the name of the class
   object_count is the number of object members
   object_members is the object_members itself (exactly equal to 
array_elems)
   */

   list ($array_params, $array_elems) = explode ('{', $serdata, 2);
   list ($array_tag, $array_count) = explode (':', $array_params, 3 );
   $serdata = O:.strlen 
($Class).:\$Class\:$array_count:{.$array_elems;

   $Instance = unserialize ( $serdata );
   return $Instance;
}
class TestClass {
   private $One=1;
   protected $Two=2;
   public $Three=3;
   public function sum() {
   return $this-One+$this-Two+$this-Three;
   }
}
$Obj = new TestClass ();
//$Clone = (array) $Obj;
$Clone = obj2array ( $Obj );
echo As the original object:br;
print_r ($Obj);
echo brbrAs an array:br;
print_r ($Clone);
$Clone[One]=7;
$Clone[Two]=7;
$Clone[Three]=7;
bless ( $Clone, TestClass );
echo brbrAfter blessing as a TestClass instance:br;
print_r ($Clone);
echo brbrCalling sum method: ;
echo $Clone-sum();
echo brThe array was blessed! miracle!!! ;-)br;
?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] bless function: a better aproach

2004-10-27 Thread Curt Zirzow
* Thus wrote Francisco M. Marzoa Alonso:
 Giving it a round, this seems to be a better aproach than the previous 
 one. It has the advantage of provide direct access to the original array 
 obtained from casting without boring about ___FAKE_KEYS_.
 
 ?
...
 
 
 $Obj = new TestClass ();
 //$Clone = (array) $Obj;
 
 $Clone = obj2array ( $Obj );

I still dont see why a simple:

  $obj = new TestClass();
  $data = serialize($obj);
  $Clone = unserialize($data);

Cant doesn't work for you.



Curt
-- 
Quoth the Raven, Nevermore.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php