Unexpected namespace behavior with closures











up vote
1
down vote

favorite












Using PHP 7.0, consider the code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
$closure();
}


To me, the expected output would be:



PHP Notice:  Undefined variable: closure


But somehow the result is



B


Then consider this code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
A$closure();
}


Now knowing (but not yet understanding) the behavior of the first example the expected output to me would be:



A


But instead I get



PHP Parse error:  syntax error, unexpected '$closure' (T_VARIABLE), expecting identifier (T_STRING)


This behavior completely confuses me.



Question part 1: can someone explain me what is wrong with my expectation in the first example?
Question part 2: how is the behavior consistent with the first example?










share|improve this question







New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
    – u_mulder
    18 hours ago












  • For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
    – AnthonyB
    17 hours ago















up vote
1
down vote

favorite












Using PHP 7.0, consider the code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
$closure();
}


To me, the expected output would be:



PHP Notice:  Undefined variable: closure


But somehow the result is



B


Then consider this code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
A$closure();
}


Now knowing (but not yet understanding) the behavior of the first example the expected output to me would be:



A


But instead I get



PHP Parse error:  syntax error, unexpected '$closure' (T_VARIABLE), expecting identifier (T_STRING)


This behavior completely confuses me.



Question part 1: can someone explain me what is wrong with my expectation in the first example?
Question part 2: how is the behavior consistent with the first example?










share|improve this question







New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
    – u_mulder
    18 hours ago












  • For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
    – AnthonyB
    17 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Using PHP 7.0, consider the code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
$closure();
}


To me, the expected output would be:



PHP Notice:  Undefined variable: closure


But somehow the result is



B


Then consider this code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
A$closure();
}


Now knowing (but not yet understanding) the behavior of the first example the expected output to me would be:



A


But instead I get



PHP Parse error:  syntax error, unexpected '$closure' (T_VARIABLE), expecting identifier (T_STRING)


This behavior completely confuses me.



Question part 1: can someone explain me what is wrong with my expectation in the first example?
Question part 2: how is the behavior consistent with the first example?










share|improve this question







New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Using PHP 7.0, consider the code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
$closure();
}


To me, the expected output would be:



PHP Notice:  Undefined variable: closure


But somehow the result is



B


Then consider this code below:



<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
A$closure();
}


Now knowing (but not yet understanding) the behavior of the first example the expected output to me would be:



A


But instead I get



PHP Parse error:  syntax error, unexpected '$closure' (T_VARIABLE), expecting identifier (T_STRING)


This behavior completely confuses me.



Question part 1: can someone explain me what is wrong with my expectation in the first example?
Question part 2: how is the behavior consistent with the first example?







php namespaces closures






share|improve this question







New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 18 hours ago









Hakariki

83




83




New contributor




Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Hakariki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
    – u_mulder
    18 hours ago












  • For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
    – AnthonyB
    17 hours ago














  • 2




    In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
    – u_mulder
    18 hours ago












  • For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
    – AnthonyB
    17 hours ago








2




2




In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
– u_mulder
18 hours ago






In a namespace you can define classes, constants and functions. Variable is not class, not function, not constant.
– u_mulder
18 hours ago














For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
– AnthonyB
17 hours ago




For your last example, I guess (but I'm not sure) that the $closure variable goes out of scope at the end of its namespace. But I don't know how you can get 'B' in your first example.
– AnthonyB
17 hours ago












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The behavior you observed should not confuse you.
It is what supposed to happen.
It is exactly how PHP namespace works.



PHP manual explains:




PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.




Not variables.



This means that $closure in your code is the exact same variable in all the namespaces you defined (A, B and C).
It is first defined in namespace A.
Then the value is replaced in namespace B.
Then you call the closure it contains in namespace C.



The second example is the same.
Because namespaces are not for grouping variables, it should be obvious that A$closure() is an invalid syntax.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Hakariki is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237533%2funexpected-namespace-behavior-with-closures%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    The behavior you observed should not confuse you.
    It is what supposed to happen.
    It is exactly how PHP namespace works.



    PHP manual explains:




    PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.




    Not variables.



    This means that $closure in your code is the exact same variable in all the namespaces you defined (A, B and C).
    It is first defined in namespace A.
    Then the value is replaced in namespace B.
    Then you call the closure it contains in namespace C.



    The second example is the same.
    Because namespaces are not for grouping variables, it should be obvious that A$closure() is an invalid syntax.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      The behavior you observed should not confuse you.
      It is what supposed to happen.
      It is exactly how PHP namespace works.



      PHP manual explains:




      PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.




      Not variables.



      This means that $closure in your code is the exact same variable in all the namespaces you defined (A, B and C).
      It is first defined in namespace A.
      Then the value is replaced in namespace B.
      Then you call the closure it contains in namespace C.



      The second example is the same.
      Because namespaces are not for grouping variables, it should be obvious that A$closure() is an invalid syntax.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        The behavior you observed should not confuse you.
        It is what supposed to happen.
        It is exactly how PHP namespace works.



        PHP manual explains:




        PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.




        Not variables.



        This means that $closure in your code is the exact same variable in all the namespaces you defined (A, B and C).
        It is first defined in namespace A.
        Then the value is replaced in namespace B.
        Then you call the closure it contains in namespace C.



        The second example is the same.
        Because namespaces are not for grouping variables, it should be obvious that A$closure() is an invalid syntax.






        share|improve this answer












        The behavior you observed should not confuse you.
        It is what supposed to happen.
        It is exactly how PHP namespace works.



        PHP manual explains:




        PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.




        Not variables.



        This means that $closure in your code is the exact same variable in all the namespaces you defined (A, B and C).
        It is first defined in namespace A.
        Then the value is replaced in namespace B.
        Then you call the closure it contains in namespace C.



        The second example is the same.
        Because namespaces are not for grouping variables, it should be obvious that A$closure() is an invalid syntax.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 15 hours ago









        Rei

        5,411623




        5,411623






















            Hakariki is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Hakariki is a new contributor. Be nice, and check out our Code of Conduct.













            Hakariki is a new contributor. Be nice, and check out our Code of Conduct.












            Hakariki is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237533%2funexpected-namespace-behavior-with-closures%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values