Re: declaring a class as stdClass?

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

 



On Wed, June 14, 2006 1:26 pm, 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;
>     }
>     ?>

In the earliest days of PHP OOP, a class (or its instance, if you
prefer) was little more than a "glorified struct" for those of you who
know C.

So, really, typecasting from an object to an array or vice-versa,
didn't really involve much of a change.

While I am sure class/object has change a lot internally, there is
still legacy code (and dinosaur coders like me) who never quite catch
up to the "new school"

You're probably gonna see this for awhile, and unless it's a
documented backwards-compatibility breaking change, it is unlikely to
change.

And, actually, the whole SPL library seems to delight in blurring the
distinction between object/array.

Then there's the funky-ass internal DOM / XML thingie that blurs it so
badly, it breaks var_dump()... :-(

At a wild guess, there are probably 1 in a zillion legitimate lines of
code where a typecast from object to array or vice versa is Good Code,
about 10,000 places it's arguable, and (zillion - 10,000 - 1) places
where it's just an abuse of the feature.

-- 
Like Music?
http://l-i-e.com/artists.htm

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