$GLOBALS and global $variable; not working in Phalanger?

Discussion about the open-source Phalanger [?] project.

$GLOBALS and global $variable; not working in Phalanger?

Postby carlosqt » March 12th, 2013, 10:12 pm

Hi there,

I'm following some PHP basics tutorial from a book and found some examples about $GLOBALS that do not seem to work in Phalanger.

When I use global $variable; and use it it does not affect the value outside the function. If I try to do the same using $GLOBALS I get a Notice message and again, the value does not get updated.

I'm using the latest release of Phalanger 3 + Phalanger tools for VS 2012.

Here below the code with 3 different examples, 2 taken from PHP documentation web site.

Code: Select all
<?php
class Program
{
    static function Main()
    {
        // Example 1               
        function updateCounter1()
        {
            global $counter;
            $counter++;
        }

        function updateCounter2()
        {
            $GLOBALS["counter"]++;               
        }
        $counter = 10;
        //updateCounter2(); // this gives an error : Notice: Undefined offset (counter) in c:\users\carlos\documents
        updateCounter1();
        echo $counter;
        // prints 10 (instead of 11)
       

        // Example 2
        function test() {
            $foo = "local variable";
            echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
            echo '$foo in current scope: ' . $foo . "\n";
        }       
        $foo = "Example content";
        test();
        // this gives an error : Notice: Undefined offset (foo) in c:\users\carlos\documents
        // and prints:
        // $foo in global scope:
        // $foo in current scope: local variable
               
        // Example 3
       
        $conf['conf']['foo'] = 'this is foo2';
        $conf['conf']['bar'] = 'this is bar';

        function foobar() {
            global $conf;
            var_dump($conf);
        }
        foobar();
        // prints NULL
       
        fgets(STDIN);

        return 0;
    }
}
?>



Is the globals functionality supported for Pure CLR phalanger programs?

Thanks in advance.

Carlos
carlosqt
 
Posts: 7
Joined: March 10th, 2013, 7:21 pm

Re: $GLOBALS and global $variable; not working in Phalanger?

Postby Jakub Misek » March 12th, 2013, 10:39 pm

Hi Carlos,

$GLOBALS magic variable works in the same way as in legacy PHP.

However pure mode is free of global code, and globals cannot be used - because of compatibility with CLR which does not have global variables.

Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: $GLOBALS and global $variable; not working in Phalanger?

Postby carlosqt » March 13th, 2013, 9:21 am

Got it.
Thanks for the info Jakub.

Carlos
carlosqt
 
Posts: 7
Joined: March 10th, 2013, 7:21 pm


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 19 guests

cron

User Control Panel

Login

Who is online

In total there are 19 users online :: 0 registered, 0 hidden and 19 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 19 guests