Re: adding objects with + equal to array process

From: Date: Wed, 05 Jul 2000 13:54:59 +0000
Subject: Re: adding objects with + equal to array process
References: 1 2 3  Groups: php.dev 
Request: Send a blank email to php-dev+get-23578@lists.php.net to get a copy of this message
waldschrott wrote: > KK> If you are treating objects like arrays, you probably > KK> should have used arrays in the first place. > > You´re right. I´m using PHP objects sometimes like arrays, for the > reason that PHP allows it, I prefer... PHP allows it, but that does not mean that it is a good choice to do it. Using PHP objects as hashes means that you get all the functionality of objects with your hashes, which is exactly what you do not need at that moment. > Ok. PHP objects *should* not be used that way, it´s my fault. I should > use arrays, then I´ve array functions available. But it´s enticing - PHP > allows it, and it even uses it. I think it is a mistake. It is at least bad style to do this, as you have experienced: It hurts when you do it. > in fact with *normal* objects which have been instanciated and weren´t > create with settype() that shouldn´t be possible That is another weakness in the language, which leads to abominations such as stdClass. PHP reports the TYPE of "$o = new C;" as "object", but does not report the CLASS of $o. That means that classes are not types and when you set the type of a variable to object, it has no class at all. What is an object? Think of it as I have outlined in a previous post, as a hash containing all instance variables plus a hidden variable "isa" pointing to the class of that object. You can serialize() that easily to (classname, name/type/value of all instance variables). What is a class? Think of it as I have outlined, as a hash containing all instance methods, plus all class variables, plus all class methods plus hidden instance variable "isa" pointing to Metaclass. At the moment you cannot properly serialize() that. One way to serialize that is to generate autoloader code for the include file defining that class, but that assumes a 1:1 relationship between classes and files. It would be much more elegant to serialize() a class as the values of all class variables, plus the compiled bytecode of all instance and class functions. That way you break the 1:1 relationship between classes and include files, you get an easy way to manipulate precompiled bytecode, and you solve the undefined class problem when using session variables, as the code of serialized objects becomes part of the session (and then you store session objects in shared memory, and the magic really happens, but that is for another discussion). Kristian -- Kristian Köhntopp, NetUSE AG Siemenswall, D-24107 Kiel Tel: +49 431 386 436 00, Fax: +49 431 386 435 99 Using PHP3? See our web development library at http://phplib.netuse.de/

« previous php.dev (#23578) next »