Mariano Guadagnini wrote:
Hi list,
I hace an existencial doubt: i've seem many scripts declaring classes
as stdClass. In the documentation (for PHP5 and also for 4), it says
that this class is internal of php, and should't be used. By the
manner I saw it's being used, i guess that it can be handy to create a
'generic' or so class, and append members when needed on the fly,
without the need for a formal class declaration, but i could't find
any good source explaining that. Has somebody some info about?
Also, along the lines of this same question, I can do this in PHP very
easily:
<?php
$data = array('apple' => 'red', 'banana' => 'yellow', 'plum' =>
'purple');
$data = (object) $data;
print_r($data);
?>
And my object is magically created for me as an instance of stdClass.
Is there a way to cast as some other type of class?
<?php
$data = array('apple' => 'red', 'banana' => 'yellow', 'plum' =>
'purple');
$data = (MyCustomClass) $data;
print_r($data);
?>
I ask this because the cast in my first example, the (object) cast is
WAY faster than using the manual approach of:
<?php
$data = array('apple' => 'red', 'banana' => 'yellow', 'plum' =>
'purple');
$MYCLASS = new MyCustomClass();
foreach ($data as $key => $value)
$MYCLASS->$key = $value;
}
?>
Dante
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php