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

  • SEARCH
  • Home
  • 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 654745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:31:40+00:00 2026-05-13T22:31:40+00:00

I need to pass the $route to its inner function,but failed: function compilePath( $route

  • 0

I need to pass the $route to its inner function,but failed:

function compilePath( $route )
{
    preg_replace( '$:([a-z]+)$i', 'pathOption' , $route['path'] );
    function pathOption($matches)
    {
        global $route;//fail to get the $route
    }
}

I’m using php5.3,is there some feature that can help?

  • 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-13T22:31:41+00:00Added an answer on May 13, 2026 at 10:31 pm

    I don’t think you can do anything like that in PHP 5.2, unfortunatly — but as you are using PHP 5.3… you could use Closures to get that to work.

    To begin, here’s a quick example of using a Closure :

    function foo()
    {
        $l = "xyz";
        $bar = function () use ($l)
        {
            var_dump($l);
        };
        $bar();
    }
    foo();
    

    Will display :

    string 'xyz' (length=3)
    

    Notice the use keyword 😉

    And here’s an example of how you could use that in your specific case :

    function compilePath( $route )
    {
        preg_replace_callback( '$:([a-z]+)$i', function ($matches) use ($route) {
            var_dump($matches, $route);
        } , $route['path'] );
    }
    
    $data = array('path' => 'test:blah');
    compilePath($data);
    

    And you’d get this output :

    array
      0 => string ':blah' (length=5)
      1 => string 'blah' (length=4)
    
    array
      'path' => string 'test:blah' (length=9)
    

    A couple of notes :

    • I used preg_replace_callback, and not preg_replace — as I want some callback function to be called.
    • I’m using an anonymous function as callback
    • And that anonymous function is importing $route, with the new use keyword.
      • Which means that, in my callback function, I can access both the matches, which are always passed by preg_replace_callback to the callback function, and the $route.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass this filepath over via route to my actionmethod: <p><a href=/Cars/@Model.CarId/@car.ContainerPath/@Model.UserId/Url>@car.Name</a></p>
I need a more-efficient way to pass the params hash to a named route,
I need to pass a subpath as a param in a route generated by
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass argument to JNLP dynamically for which I tried using a
I need to pass Cursor object to another activity, what is the best way
I need to pass a variable which is captured from client side via jquery
I need to pass a parameter from an EditText and when I click the
I need to pass a simple Javascript array to my wcf ajax webservice: var
I need to pass a pointer through a scripting language which just has a

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.