php-general Digest 14 Mar 2012 03:01:54 -0000 Issue 7727

Topics (messages 317004 through 317013):

Re: questions about $_SERVER
        317004 by: Tedd Sperling
        317006 by: Tedd Sperling
        317007 by: Matijn Woudt
        317008 by: Stuart Dallas
        317009 by: Tedd Sperling
        317010 by: Tim Streater
        317013 by: Donovan Brooke

Randomly unable to read set variable from class
        317005 by: Adrian Basalic
        317011 by: Camilo Sperberg

set_error_handler() only triggering every Nth time
        317012 by: Daevid Vincent

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Mar 12, 2012, at 7:12 PM, Tim Streater wrote:
> <?php
> 
> function yes ($a)
>     {
>     global $x;
>     if  ($a)  $x = "yes\n";
>     }
> 
> first (true);
> 
> echo $x;
> 
> ?>
> 
> 
> but I haven't looked into $GLOBALS enough to know whether using them instead 
> would have saved my bacon.

I'm not sure what would have saved bacon in the above case. I don't see how 
your example would work. I think it contained a typo.

In what I think you were trying to demonstrate, I would just pass $x by 
reference (&$x) -- or -- return $x by value. I would not have used a global,

In any event, I seldom use globals anyway. This was more an academic discussion.

Cheers,

tedd

_____________________
tedd.sperl...@gmail.com
http://sperling.com

--- End Message ---
--- Begin Message ---
On Mar 12, 2012, at 12:04 PM, Daniel Brown wrote:
> On Sun, Mar 11, 2012 at 14:16, Tedd Sperling <tedd.sperl...@gmail.com> wrote:
>> This document clearly states that $GLOBALS is a SuperGlobal -- what am I not 
>> understanding here?
> 
>    You are understanding it correctly, the only thing that's missing
> is the population.  The variables are defined (set), but not all are
> populated.  $GLOBALS is a superglobal, you're right; globals set from
> userland scripts are not superglobals, but do wind up in the $GLOBALS
> array.  Thus, all superglobals are globals, but not all globals are
> superglobals.

So, it's a question of population timing -- I see.

Additionally, I like the term "userland" -- I will use it in class. :-)

What would be the opposite term, serverland?

Thanks,

tedd

_____________________
tedd.sperl...@gmail.com
http://sperling.com

--- End Message ---
--- Begin Message ---
On Tue, Mar 13, 2012 at 4:59 PM, Tedd Sperling <tedd.sperl...@gmail.com> wrote:
> On Mar 12, 2012, at 7:12 PM, Tim Streater wrote:
>> <?php
>>
>> function yes ($a)
>>     {
>>     global $x;
>>     if  ($a)  $x = "yes\n";
>>     }
>>
>> first (true);
>>
>> echo $x;
>>
>> ?>
>>
>>
>> but I haven't looked into $GLOBALS enough to know whether using them instead 
>> would have saved my bacon.
>
> I'm not sure what would have saved bacon in the above case. I don't see how 
> your example would work. I think it contained a typo.
>
> In what I think you were trying to demonstrate, I would just pass $x by 
> reference (&$x) -- or -- return $x by value. I would not have used a global,
>
> In any event, I seldom use globals anyway. This was more an academic 
> discussion.
>
> Cheers,
>
> tedd
>

I would indeed mark it as bad practice using them. I only use them for
debugging purposes. When developing something, you might end up
needing some global variable temporary, and you don't want to pass it
through a few dozen functions before reaching the one where you need
it.

- Matijn

--- End Message ---
--- Begin Message ---
On 13 Mar 2012, at 15:59, Tedd Sperling wrote:

> In any event, I seldom use globals anyway. This was more an academic 
> discussion.

If you're being academic about it please remember that the way PHP defines 
globals is different to most other languages.

PHP: A variable defined at the top-level scope.

World: A variable that is visible at every scope.

This is an important difference if you ever move from PHP to another language. 
It ultimately also means that only the superglobals are true globals.

The $GLOBALS superglobal contains all variables defined at the top-level scope, 
including $GLOBALS, so $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['_SERVER'] is 
a perfectly valid, if daft, way of accessing $_SERVER.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
On Mar 13, 2012, at 12:20 PM, Stuart Dallas wrote:
> On 13 Mar 2012, at 15:59, Tedd Sperling wrote:
> 
>> In any event, I seldom use globals anyway. This was more an academic 
>> discussion.
> -snip-
> It ultimately also means that only the superglobals are true globals.

That was my initial statement in this thread.

After 47 years of programming, I think I'm beginning to get the idea. :-)

As I've said for many years "I've learned something new every day of my life -- 
and I'm getting damned tried of it."

Cheers,

tedd

_____________________
tedd.sperl...@gmail.com
http://sperling.com

--- End Message ---
--- Begin Message ---
On 13 Mar 2012 at 15:59, Tedd Sperling <tedd.sperl...@gmail.com> wrote: 

> I'm not sure what would have saved bacon in the above case. I don't see how
> your example would work. I think it contained a typo.
>
> In what I think you were trying to demonstrate, I would just pass $x by
> reference (&$x) -- or -- return $x by value. I would not have used a global,
>
> In any event, I seldom use globals anyway. This was more an academic
> discussion.

As was my example - and yes, it had a typo. Worse, trying it in a shell it 
doesn't exhibit the failure mode I thought I'd had. Never mind.

--
Cheers  --  Tim

--- End Message ---
--- Begin Message ---
Stuart Dallas wrote:
[snip] so $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['_SERVER'] is a perfectly 
valid, if daft, way of accessing $_SERVER.

-Stuart



Now this is becoming educational! ;-)

Donovan


--
D Brooke

--- End Message ---
--- Begin Message ---
I have an issue that occurs randomly on a machine. After a number of reloads
of the page i can't read $this->_foo although it is set (var_dump shows it
but the script cannot read it). The code works locally and on other machines
i tested. Apache restart seems to fix it but only temporary.

Environment:

.         PHP Version 5.3.3

.         Linux 2.6.32-220.2.1.el6.x86_64 #1 SMP Fri Dec 23 02:21:33 CST
2011 x86_64

.         Apache 2.0

 

class  FooBar

    {

      protected $_foo;

      public function setFoo($bar)

      {

        if (!$bar) {

          print_r("Cannot find bar");

        }

        $this->_foo = $bar;

        if (!$this->_foo) {

          print_r("Cannot read {$this->_foo} set with $bar");

          var_dump($this);

        }

        var_dump($this);

        return $this;

      }

    }

 

    $foobar = new FooBar;

    $foobar->setFoo('bar');

 

The output when this happens would be:

 

Cannot read set with barobject(FooBar)#1 (1) { ["_foo":protected]=>
string(3) "bar" } object(FooBar)#1 (1) { ["_foo":protected]=> string(3)
"bar" }

 

I'm going nuts here, and nobody seems to be able to reproduce this. Where
should I start looking?


--- End Message ---
--- Begin Message ---
Sorry wasn't able to reproduce it on my test machine. 

Do you have any external modules loaded such as APC, memcached, xDebug maybe or 
other that can affect the output on that machine?

Greetings

On 13 Mar 2012, at 17:02, Adrian Basalic wrote:

> I have an issue that occurs randomly on a machine. After a number of reloads
> of the page i can't read $this->_foo although it is set (var_dump shows it
> but the script cannot read it). The code works locally and on other machines
> i tested. Apache restart seems to fix it but only temporary.
> 
> Environment:
> 
> .         PHP Version 5.3.3
> 
> .         Linux 2.6.32-220.2.1.el6.x86_64 #1 SMP Fri Dec 23 02:21:33 CST
> 2011 x86_64
> 
> .         Apache 2.0
> 
> 
> 
> class  FooBar
> 
>    {
> 
>      protected $_foo;
> 
>      public function setFoo($bar)
> 
>      {
> 
>        if (!$bar) {
> 
>          print_r("Cannot find bar");
> 
>        }
> 
>        $this->_foo = $bar;
> 
>        if (!$this->_foo) {
> 
>          print_r("Cannot read {$this->_foo} set with $bar");
> 
>          var_dump($this);
> 
>        }
> 
>        var_dump($this);
> 
>        return $this;
> 
>      }
> 
>    }
> 
> 
> 
>    $foobar = new FooBar;
> 
>    $foobar->setFoo('bar');
> 
> 
> 
> The output when this happens would be:
> 
> 
> 
> Cannot read set with barobject(FooBar)#1 (1) { ["_foo":protected]=>
> string(3) "bar" } object(FooBar)#1 (1) { ["_foo":protected]=> string(3)
> "bar" }
> 
> 
> 
> I'm going nuts here, and nobody seems to be able to reproduce this. Where
> should I start looking?
> 


_______________________
Mi blog
CHW
Mi Twitter


--- End Message ---
--- Begin Message ---
I am implementing a custom error handler and started noticing some bizarre
behavior. Every Nth time I refresh the page, I see the error/output.

In my 'includes/common.inc.php' the key things are these:

        set_error_handler('php_error_handler');

        function php_error_handler($errno, $errstr, ...)
        {
           //does some stuff
           //calls php_custom_error_log()
        }

        function php_custom_error_log()
        {
                echo "php_custom_error_log : ".date('H:i:s');
        
                if ($error = error_get_last())
                {
                        var_dump(LOG_LEVEL, $error);
        
                //does more stuff
        }

My test page:

        <?php
        define('LOG_LEVEL', E_ALL ^ E_NOTICE);
        error_reporting(LOG_LEVEL);
        ini_set('display_errors','On');
        
        define('VM', true);
        define('DEVELOPMENT', false);
        
        ini_set('xdebug.var_display_max_children', 1000 ); 
        ini_set('xdebug.var_display_max_depth', 5 ); 
        
        require_once 'includes/common.inc.php';
        
        ###### BEGIN TEST #########
        
        foreach($empty as $k) echo $k;
        ?>

For those astute observers, you'll note that $empty is not an array and
therefore I expect an error message (well warning)

( ! ) Warning: Invalid argument supplied for foreach() in
/usr/home/vz/examples.videosz.com/error_handler_tests.php on line 20
Call Stack
#       Time    Memory  Function        Location
1       0.0005  663616  {main}( )       ../error_handler_tests.php:0

As I simply click 'refresh' on the browser I noticed that it was only
periodically working. I then deduced that it was directly related to the
number of apache threads. So if I had 15 then it would work every 15th
refresh:

developer@vm:/usr/local/etc/apache22$ ps aux | grep httpd
(standard input):39:root      15855  0.0  2.0 204156 20292  ??  Ss   28Feb12
1:33.48 /usr/local/sbin/httpd -k start
(standard input):48:www       89522  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):49:www       89523  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):50:www       89524  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):51:www       89525  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):52:www       89527  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):53:www       89528  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):54:www       89529  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):55:www       89530  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):56:www       89531  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):57:www       89532  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):58:www       89533  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):59:www       89534  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):60:www       89535  0.0  2.0 204156 20332  ??  I     8:03PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):61:www       89563  0.0  2.1 206204 21700  ??  I     8:17PM
0:00.10 /usr/local/sbin/httpd -k start
(standard input):62:www       89578  0.0  2.0 204156 20332  ??  I     8:22PM
0:00.01 /usr/local/sbin/httpd -k start
(standard input):74:developer 89587  0.0  0.1  9092  1196   1  S+    8:30PM
0:00.01 grep -inH --color httpd
 

124 <IfModule prefork.c>
125 StartServers      15
126 MinSpareServers   15
127 MaxSpareServers   20
128 ServerLimit       50
129 MaxClients        50
130 MaxRequestsPerChild  15
131 KeepAlive         Off
132 </IfModule>

Just to check and the time is updating each press and every 15th try I'd get
this:

php_custom_error_log : 19:54:20
int 30711
array
  'type' => int 8192
  'message' => string 'Directive 'register_globals' is deprecated in PHP 5.3
and greater' (length=65)
  'file' => string 'Unknown' (length=7)
  'line' => int 0

When I'd expect to see a message every time (with a new timestamp).

Then I REALLY looked at the 'message' part. "register_globals". WTF? That
isn't the error I expected.

Hmmm.. what happens if I move the second function inline to the first...

BAM! EVERY TIME it works perfectly.

So WTFF??! Why does moving it inline make any difference? All I suspect is
the $error = error_get_last() part. For some reason that isn't consistent.
And the other strange thing is that it's giving me erroneous error messages
(well, we do have register_globals on, but that's not the error I was
expecting).

Then I did another test replacing the foreach() with this statement:

        require_once "/foo.inc.php";

A few bizarre things about this too. For starters why do I get TWO error
messages. One WARNING from MY error handler and then a FATAL default PHP
one?! Sorta defeats my purpose.

        WARNING: require_once(/foo.inc.php) [function.require-once]:
                 failed to open stream: No such file or directory in
                 /usr/home/vz/examples.com/error_handler_tests.php on line
22

        Fatal error: require_once() [function.require]:
                 Failed opening required '/foo.inc.php'
(include_path='.:/home/SHARED')
                 in /usr/home/examples. com/error_handler_tests.php on line
22

Next if I print out php_error_handler($errno, ...) it says "2". And '2' is a
WARNING according to this page:
http://php.net/manual/en/errorfunc.constants.php Shouldn't it be "1"!?

developer@vm_daevid:/usr/local/etc/apache22$ php --version
PHP 5.3.6 with Suhosin-Patch (cli) (built: Apr 28 2011 14:20:48)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans

Running on FreeBSD.


--- End Message ---

Reply via email to