Voting

: min(seven, eight)?
(Example: nine)

The Note You're Voting On

fmmarzoa at librexpresion dot org
20 years ago
You can still cast the object to an array to get all its members and see its visibility. Example:

<?php

class Potatoe {
public
$skin;
protected
$meat;
private
$roots;

function
__construct ( $s, $m, $r ) {
$this->skin = $s;
$this->meat = $m;
$this->roots = $r;
}
}

$Obj = new Potatoe ( 1, 2, 3 );

echo
"<pre>\n";
echo
"Using get_object_vars:\n";

$vars = get_object_vars ( $Obj );
print_r ( $vars );

echo
"\n\nUsing array cast:\n";

$Arr = (array)$Obj;
print_r ( $Arr );

?>

This will returns:

Using get_object_vars:
Array
(
[skin] => 1
)

Using array cast:
Array
(
[skin] => 1
[ * meat] => 2
[ Potatoe roots] => 3
)

As you can see, you can obtain the visibility for each member from this cast. That which seems to be spaces into array keys are '\0' characters, so the general rule to parse keys seems to be:

Public members: member_name
Protected memebers: \0*\0member_name
Private members: \0Class_name\0member_name

I've wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object:

<?php

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;
}

?>

I've created also a <i>bless</i> function that works similar to Perl's bless, so you can further recast the array converting it in an object of an specific class:

<?php

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 );

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;
}
?>

With these ones you can do things like:

<?php

define
("SFCMS_DIR", dirname(__FILE__)."/..");
require_once (
SFCMS_DIR."/Misc/bless.php");

class
Potatoe {
public
$skin;
protected
$meat;
private
$roots;

function
__construct ( $s, $m, $r ) {
$this->skin = $s;
$this->meat = $m;
$this->roots = $r;
}

function
PrintAll () {
echo
"skin = ".$this->skin."\n";
echo
"meat = ".$this->meat."\n";
echo
"roots = ".$this->roots."\n";
}
}

$Obj = new Potatoe ( 1, 2, 3 );

echo
"<pre>\n";
echo
"Using get_object_vars:\n";

$vars = get_object_vars ( $Obj );
print_r ( $vars );

echo
"\n\nUsing obj2array func:\n";

$Arr = obj2array($Obj);
print_r ( $Arr );

echo
"\n\nSetting all members to 0.\n";
$Arr['skin']=0;
$Arr['meat']=0;
$Arr['roots']=0;

echo
"Converting the array into an instance of the original class.\n";
bless ( $Arr, Potatoe );

if (
is_object ($Arr) ) {
echo
"\$Arr is now an object.\n";
if (
$Arr instanceof Potatoe ) {
echo
"\$Arr is an instance of Potatoe class.\n";
}
}

$Arr->PrintAll();

?>

<< Back to user notes page

To Top