Re: declaring a class as stdClass?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To answer both of you:

$var = new stdClass();

is perfectly acceptable.  I do it all the time.  The caveat is that the 
behavior is slightly different in PHP 4 than PHP 5.

In PHP 4, $var would behave exactly like an associative array, just with 
different funky characters to access its properties.  Internally, in fact, 
they're represented the same.

In PHP 5, $var would behave exactly like an associative array, EXCEPT when 
passing it as a parameter to a function.  In PHP 4, objects pass by value by 
default.  In PHP 5, they pass by reference by default.

Also, yes, casting an object to an array or vice versa is much faster than 
manually iterating it.  So:

$var = (object)array('a'=>'foo', 'b'=>'bar', 'c'=>'baz');

Is perfectly acceptable.  I've never tried casting to some other type of 
class, though.  Give it a try and let us know how it goes. :-)

On Wednesday 14 June 2006 13:26, D. Dante Lorenso wrote:
> 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

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux