php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62169 Use 'global' like a language structure
Submitted: 2012-05-26 20:31 UTC Modified: 2013-07-28 18:06 UTC
From: valentiny510 at yahoo dot es Assigned:
Status: Wont fix Package: *Programming Data Structures
PHP Version: 5.4.3 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
22 - 4 = ?
Subscribe to this entry?
Further comment on this bug is unnecessary.
 
 [2012-05-26 20:31 UTC] valentiny510 at yahoo dot es
Description:
------------
Imagine I have the, unset ( $GLOBAL )..
To access a 'global' object into the function, first must be called trowght global..

function test()
{
    global $object;
    return $object->some_method( );
}

I wonder if is posible to use (in the future :P) the 'global' like other language structures.. Ex:

function test()
{
    return( global $object->some_method( ) );
}

I know ... you will ask me why not use the namespaces, reflections, references, traits, or whatever.. but .. its not the point ...



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-30 13:35 UTC] riptide dot tempora at opinehub dot com
For now:
<?php
function test()
{
    return( $GLOBALS['object']->some_method( ) );
}
?>
 [2012-06-02 00:59 UTC] valentiny510 at yahoo dot es
In the first line I just wrote: "Imagine I have unset the ($GLOBALS)"

for some reason.. (is ugly, security, memory consumption, the host provider doesn't allow) or who knows why ...

I know how it works for now but the question is not about how to use "$GLOBALS".

anyway, thanks for the time spent to answer
 [2012-06-02 01:01 UTC] valentiny510 at yahoo dot es
Imagine I have unset the, ( $GLOBALS )..
To access a 'global' object into the function, first must be called trowght global..

function test()
{
    global $object;
    return $object->some_method( );
}

I wonder if is posible to use (in the future :P) the 'global' like other language structures.. Ex:

function test()
{
    return( global $object->some_method( ) );
}

I know ... you will ask me why not use the namespaces, reflections, references, traits, or whatever.. but .. its not the point ...
 [2012-10-21 21:06 UTC] joost dot koehoorn at gmail dot com
My solution would be to introduce global as a scope identifier, so you could use 
it as:

global::$object = new stdClass;
global::$object->test = 'This is just a test';

function test()
{
    return global::$object->test;
}


Although I do believe globals are evil and static class variables should be used 
at all times, this is a neat addition to avoid the very ugly and unclear global 
keyword.

As it is now, you have to define a variable as global for it to resolve and act on 
the global variable. This makes code hard to read as it is not directly visible 
that this is the case. The above syntax would solve this problem.
 [2012-10-21 22:07 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2012-10-21 22:07 UTC] nikic@php.net
As already noted in this thread, using globals is highly discouraged and as such it does not make sense to add any further functionality improving their use.

Also (you say this yourself) you can use $GLOBALS. I don't understand why you don't want to use it. Language features aren't added simply because someone says "Hey, I want to access globals in one expression, but I don't want to use $GLOBALs, please add a feature that is fully equivalent but uses a different syntax".

But even with the constraint of not using $GLOBALS you can easily build a function that would do something equivalent:

    function getGlobal($name) {
        global $$name;
        return $$name;
    }

    $foo = getGlobal('foo');

Marking this as Wontfix.
 [2013-07-28 16:50 UTC] valentiny510 at yahoo dot es
Nikic you are really a php developer ?
Rasmus should choose better his team..
 [2013-07-28 17:02 UTC] valentiny510 at yahoo dot es
Joost if you can explain the 'evil' difference between this 2 examples and also tell me whitch is more 'hard to read' and why...
I will give you the Nobel prize por programming...

    function test( ) {
        return global $variable;
    }

    function test( ) {
        global $variable;
        return $variable;
    }

and... nobody asked you about your 'solution' with scope identifier
global:: can be more evil yes.. but I do not care about your opinion, sorry
 [2013-07-28 18:06 UTC] johannes@php.net
-Block user comment: No +Block user comment: Yes
 [2013-07-28 18:06 UTC] johannes@php.net
And we not about yours.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC