Re: [PHP] declaring a class as stdClass?

2006-06-15 Thread Richard Lynch
On Wed, June 14, 2006 1:08 pm, Mariano Guadagnini wrote:
 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?

When you do:

class foo { };

I believe that, down in the guts, foo is a subclass of stdClass.

Now, assuming you want to be all loosy-goosy and not really hard-core
other-language purist OOP...

$foo = new stdClass;

is not really significantly different from:

class foo { }; $foo = new foo;

So, put it this way:  If your OOP inhibitions are loose enough to want
to use stdClass in the first place, and then start throwing data into
it like:

$foo-undeclared_member_variable = 42;

Then, really, I don't think using stdClass is gonna be a Big Issue,
compared to what you are already doing.

This is merely my opinion, and is not meant to reflect the Documented
Featureset, and certainly not the current direction Internals seems to
be taking with OOPurism.

Since I never actually use class (much) in the first place, I don't
really care either way. :-)

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



Re: [PHP] declaring a class as stdClass?

2006-06-15 Thread Richard Lynch
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



[PHP] declaring a class as stdClass?

2006-06-14 Thread Mariano Guadagnini

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?


Thanks in advance.


Mariano Guadagnini.-


--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.8.4/363 - Release Date: 13/06/2006

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



Re: [PHP] declaring a class as stdClass?

2006-06-14 Thread D. Dante Lorenso

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



Re: [PHP] declaring a class as stdClass?

2006-06-14 Thread Larry Garfield
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
[EMAIL PROTECTED]   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