Re: [PHP] DOMNode children iteration (was Re: array() returns something weird)

2009-08-26 Thread Szczepan Hołyszewski
Martin Scotta wrote:

 Fatal error: Call to a member function getAttribute() on a non-object in
 testme.php on line *121*

Yes, this is _how_ the unmodified script errors out. It is not shown in the 
output I attached in my previous message because display_errors is Off in my 
php.ini, and the line that sets it to On in my script is commented out.

 I'm using a Apache/2.2.3 (Win32) PHP/5.2.6

I am not using Apache at all. The same error can be seen when I run the script 
directly through php on the command line.

 Is the script correctly? I have removed the  in the loop

I know that iterating a DOMNode's children using firstChild and nextSibling by 
reference causes trouble, thank you. The problem is that it causes trouble 
with objects unrelated to the loop itself, e.g. to variables defined _after_ 
the loop has terminated.

 and the script returns the attached file.

Do you mean it returns what is in the output file _I_ have attached, or did 
_you_ try to attach something? If that is the case, then your attachment 
didn't get through.

 It looks like a bogus bug?

I am certain it _is_ a genuine bug. For example, in PHP when you write:

$foo = array();
echo gettype($foo);

the output should not be NULL, but in my example script (unmodified, with 
iteration by reference) things like this start happening at some point.

Regards,
Szczepan Hołyszewski

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



RE: [PHP] DOMNode children iteration (was Re: array() returns something weird)

2009-08-26 Thread Ford, Mike
 -Original Message-
 From: Szczepan Hołyszewski [mailto:webmas...@strefarytmu.pl]
 Sent: 26 August 2009 08:48
 
 Martin Scotta wrote:
 
  Fatal error: Call to a member function getAttribute() on a non-
 object in
  testme.php on line *121*
 
 Yes, this is _how_ the unmodified script errors out. It is not shown
 in the
 output I attached in my previous message because display_errors is
 Off in my
 php.ini, and the line that sets it to On in my script is commented
 out.
 
  I'm using a Apache/2.2.3 (Win32) PHP/5.2.6
 
 I am not using Apache at all. The same error can be seen when I run
 the script
 directly through php on the command line.
 
  Is the script correctly? I have removed the  in the loop

I really can't see why you'd want to use references in the posted loop anyway 
-- it's not likely to gain you any performance, and, as I understand it, may 
actually run more slowly than the non-reference version.
 
 I know that iterating a DOMNode's children using firstChild and
 nextSibling by
 reference causes trouble, thank you. The problem is that it causes
 trouble
 with objects unrelated to the loop itself, e.g. to variables defined
 _after_
 the loop has terminated.

I don't think it's possible to answer this for sure without seeing more of the 
code -- at least the complete body of the loop, plus as far as any further use 
of the variable $child (and any other variables assigned from it).


Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.f...@leedsmet.ac.uk 
Tel: +44 113 812 4730




To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm


Re: [PHP] DOMNode children iteration (was Re: array() returns something weird)

2009-08-26 Thread Szczepan Hołyszewski
 I really can't see why you'd want to use references in the posted
 loop anyway -- it's not likely to gain you any performance, and, as
 I understand it, may actually run more slowly than the
 non-reference version.

The why is irrelevant. It is perfectly legal PHP, and while it might be 
illegal usage of DOMNode, it should only break the DOMNode, and not PHP 
execution environment.

 I don't think it's possible to answer this for sure without seeing more of
 the code -- at least the complete body of the loop, plus as far as any
 further use of the variable $child (and any other variables assigned from
 it).

OK, I modified the example to show the problem _very_ directly. I commented out 
the recursion-tracing echos, and added this code in renderObject() below the 
if() that chooses between the good and evil loop, which means that the added 
code is always executed immediately after the loop:

$x=array();
$y=y;
$z=1;
var_dump($xy,$y,$z);
echo \n;

Note that these variables:

 - are first defined at this point
 - are initialized immediately
 - are initialized with immediate data (empty array and two scalars), not
   with results of prior computation
 - are never used or changed except for being dumped
 - don't get any references to them created

Therefore one must expect that var_dump will always dump this:

array(0) { 
}  
string(1) y  
int(1)

However it sometimes dumps this:

NULL   
NULL   
NULL

Best regards,
Szczepan Holyszewski
attachment: testme.php
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[PHP] DOMNode children iteration (was Re: array() returns something weird)

2009-08-25 Thread Martin Scotta
-- Forwarded message --
From: Martin Scotta martinsco...@gmail.com
Date: Tue, Aug 25, 2009 at 6:24 PM
Subject: Re: [PHP] DOMNode children iteration (was Re: array() returns
something weird)
To: webmas...@strefarytmu.pl


Fatal error: Call to a member function getAttribute() on a non-object in
testme.php on line *121*

I'm using a Apache/2.2.3 (Win32) PHP/5.2.6
Is the script correctly? I have removed the  in the loop and the script
returns the attached file.

It looks like a bogus bug?

2009/8/24 Szczepan Hołyszewski webmas...@strefarytmu.pl


 Hi Lars and list!

 New facts about the strange bug initially diagnosed as array() returning
 NULL:

 Firstly, the issue is not really about array() returning null, but about
 full
 blown UndefinedBehavior(TM) progressively trashing local variables. It just
 so
 happened that I noticed it first with a variable to which array() had been
 assigned just before things began breaking.

 Secondly, I proudly present the culprit: things break when I iterate over a
 DOMNode's children *by reference* using firstChild and nextSibling:

for($child=$node-firstChild; $child; $child=$child-nextSibling)
 {

//processing...
}

 No problems if iteration is done by value or by DOMNodeList (childNodes,
 DOMXPath...).

 HOWEVER,

 I still consider this a bug because it destablizes PHP. After the evil loop
 has finished, things happen that should never happen, like a variable being
 NULL immediately after being assigned array().

 I attach a demonstration. It is a self-contained commented script that you
 can
 execute from command line. Also attached is output on my machine.

 Best regards,
 Szczepan Hołyszewski

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




-- 
Martin Scotta



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

[PHP] DOMNode children iteration (was Re: array() returns something weird)

2009-08-24 Thread Szczepan Hołyszewski

Hi Lars and list!

New facts about the strange bug initially diagnosed as array() returning NULL:

Firstly, the issue is not really about array() returning null, but about full 
blown UndefinedBehavior(TM) progressively trashing local variables. It just so 
happened that I noticed it first with a variable to which array() had been 
assigned just before things began breaking.

Secondly, I proudly present the culprit: things break when I iterate over a 
DOMNode's children *by reference* using firstChild and nextSibling:

for($child=$node-firstChild; $child; $child=$child-nextSibling) {

//processing...
}

No problems if iteration is done by value or by DOMNodeList (childNodes, 
DOMXPath...).

HOWEVER,

I still consider this a bug because it destablizes PHP. After the evil loop 
has finished, things happen that should never happen, like a variable being 
NULL immediately after being assigned array().

I attach a demonstration. It is a self-contained commented script that you can 
execute from command line. Also attached is output on my machine.

Best regards,
Szczepan Hołyszewski
attachment: testme.php
THE GOOD WAY (DOMNodeList):

Recursion structure:

-render()
-render()
-render()
-
-render()
-
-
-

Transformation result:

Some text.
[object type='foo' name='bar'][param name='__default_content']
Some parameter text.
Some parameter text.
[object type='bar' name='nested'][param name='__default_content']
Some nested parameter text.
Some nested parameter text.
[object type='baz' name='deeply_nested'][param 
name='__default_content']
[/param][param name='bold']true[/param][/object]
More nested parameter text
More nested parameter text
[/param][/object]
[/param][/object]
Some more text

Success!

THE MIXED WAY (DOMNodeList at outermost level, then firstChild/nextSibling by 
ref):

Recursion structure:

-render()
-render()
-render()
-
-render()
-
-
-

Transformation result:

Some text.
[object type='foo' name='bar'][param name='__default_content']
Some parameter text.
Some parameter text.
__default_content[param 
name='__default_content']__default_content[/param][/object]
[/param][/object]
Some more text

Is it really a success?

THE EVIL WAY (firstChild/nextSibling by ref):

Recursion structure:

-render()
-render()
-render()
-
-render()
-

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