|
Posted by Oliver Grδtz on 03/01/07 03:41
denisbytezone@gmail.com schrieb:
> I've hit two problems recently that strike me as major issues.
> Firstly, if you compare two objects for equality, and there is some
> recursion involved internal to the object's structure, then PHP gives
> an error.
>
> Consider this:
>
> $blob1 = new Blob ();
> $blob2 = new Blob ();
>
> $blob1->child = $blob2;
> $blob2->child = $blob1;
>
> $blob1 === $blob2 ? print 'equal' : print 'not equal'; // works
> $blob1 == $blob2 ? print 'equal' : print 'not equal'; // fails
>
> Class Blob
> {
> public $child;
> }
>
> I reported this as a bug, and was told "that's how PHP works - not a
> bug"
You did not say what your script outputs. Please remember to always do
that in the future. Just saying "works" doesn't help if you and the
reader have different assumptions as to what the result should be.
The first comparison checks if the objects are identical. This can be
answered with no just by looking at the main variable hash tables. They
are stored in different locations => not equal.
The first comparison checks if the objects are equal, as in "they
evaluate to the same". Evaluation is a problem here, because blob1 has
blob2 as child, which has blob1 as child, which has... and so on. The
objects can never be fully evaluated so PHP can never tell if they
evaluate to the same. No error here.
The == can be a problem if you don't know just how sloppy it is. For
example, (2=="2blob") is true because the comparison for int and string
is the comparison of the integer values of both arguments and the string
"2blob" evaluates to 2.
> Secondly, today I find out that using session_start() is a good way to
> pass objects from web page to page, UNLESS they use SimpleXML. As they
> say, WTF?
Not every object can be serialized (represented as a string), and this
is required for storing it in a session. Java shares this problem,
objects to be serialized must be instances of classes implementing the
Serializable interface. The problem is due to some classes being engine
internal, their state is not fully stored in PHP user space. This
improves the performance of such classes.
> Is there a workaround to the second problem? How does one share
> objects between pages when using SimpleXML?
You can try to export XML from your object, store that in the session,
and later reload it to a new object from this string. Even then,
remember that sessions are not meant for storing really big amounts of
data. Your data has to be exported as a giant serialized array and later
be reimported. The default way of storing a session is to save this
string to a plain text file! On the next request, this file has to be
read in and the string has to be parsed into an array. This is a big
potential performance bottleneck for your application. Try to store most
data in databases.
OLLi
____________
"You mess with a friend of Flinkman? You're messin' with Flinkman."
[Marshall, Alias 408]
Navigation:
[Reply to this message]
|