Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:36:09+00:00 2026-05-25T15:36:09+00:00

What are the differences between closures in JS and closures in PHP? Do they

  • 0

What are the differences between closures in JS and closures in PHP? Do they pretty much work the same way? Are there any caveats to be aware of when writing closures in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:36:09+00:00Added an answer on May 25, 2026 at 3:36 pm

    One difference is how both cope with storing the context in which an anonymous function is executed:

    // JavaScript:
    var a = 1;
    var f = function() {
       console.log(a);
    };
    a = 2;
    f();
    // will echo 2;
    
    // PHP
    $a = 1;
    $f = function() {
        echo $a;
    };
    $a = 2;
    $f();
    // will result in a "PHP Notice:  Undefined variable: a in Untitled.php on line 5"
    

    To fix this notice you’ll have to use the use syntax:

    $a = 1;
    $f = function() use ($a) {
        echo $a;
    };
    $a = 2;
    $f();
    // but this will echo 1 instead of 2 (like JavaScript)
    

    To have the anonymous function behave somehow like the JavaScript counterpart you’ll have to use references:

    $a = 1;
    $f = function() use (&$a) {
        echo $a;
    };
    $a = 2;
    $f();
    // will echo 2
    

    I think this is the most striking difference between JavaScript and PHP closures.

    Second difference is that every JavaScript closure has a this context available which means, that you can use this inside the closure itself (although it’s often quite complicated to figure out what this actually refers to) – PHP’s current stable version (PHP 5.3) does not yet support $this inside a closure, but PHP’s upcoming version (PHP 5.4) will support $this binding and rebinding using $closure->bind($this) (See the Object Extension RFC for more info.)

    Third difference is how both languages treat closures assigned to object properties:

    // JavaScript
    var a = {
        b: function() {}
    };
    a.b(); // works
    
    
    // PHP
    $a = new stdClass();
    $a->b = function() {};
    $a->b(); // does not work "PHP Fatal error:  Call to undefined method stdClass::b() in Untitled.php on line 4"
    
    $f = $a->b;
    $f(); // works though
    

    The same is true if closures are assigned to properties in class definitions:

    class A {
        public $b;
    
        public function __construct() {
            $this->b = function() {};
        }
    
        public function c() {
            $this->b();
        }
    }
    $a = new A();
    // neither
    $a->b();
    // nor
    $a->c();
    // do work
    

    Fourth difference: JavaScript Closures are full fledged objects, wheres in PHP they are restricted objects. For instance, PHP Closures cannot have properties of their own:

    $fn = function() {};
    $fn->foo = 1;
    // -> Catchable fatal error: Closure object cannot have properties
    

    while in JavaScript you can do:

    var fn = function() {};
    fn.foo = 1;
    fn.foo; // 1
    

    Fifth difference: Returned closures can be immediately called upon in Javascript:

    var fn = function() { return function() { alert('Hi');}}
    fn()();    
    

    Not in PHP:

    $fn = function() { return function() { echo('Hi');};};
    $fn()();     // syntax error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any differences between a user in SQL Server and one in Oracle?
Are there any differences between what in Common Lisp you'd call an atom, and
Are there any differences between boost::shared_ptr , std::tr1::shared_ptr and the upcoming (in C++0x )
Is there any differences between hibernate session and the session which you use while
Are there any practical differences between these two ways of getting an exception for
Are there any important differences between declaring <script type=text/javascript> </script> and <script language=javascript> </script>
What are the differences between mod_php and cgi php script? I mean, why it's
what are some key differences between mvc1 and mvc2. I have to adopt any
Are there philosophical differences between Smalltalk OOP and Simula OOP ? This is a
What are the differences between SHA1 and RSA? Are they just different algorithms or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.