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 486689
In Process

The Archive Base Latest Questions

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

Is it possible to use the equivalent for .NET method attributes in PHP, or

  • 0

Is it possible to use the equivalent for .NET method attributes in PHP, or in some way simulate these?

Context

We have an in-house URL routing class that we like a lot. The way it works today is that we first have to register all the routes with a central route manager, like so:

$oRouteManager->RegisterRoute('admin/test/', array('CAdmin', 'SomeMethod'));
$oRouteManager->RegisterRoute('admin/foo/', array('CAdmin', 'SomeOtherMethod'));
$oRouteManager->RegisterRoute('test/', array('CTest', 'SomeMethod'));

Whenever a route is encountered, the callback method (in the cases above they are static class methods) is called. However, this separates the route from the method, at least in code.

I am looking for some method to put the route closer to the method, as you could have done in C#:

<Route Path="admin/test/">
public static void SomeMethod() { /* implementation */ }

My options as I see them now, are either to create some sort of phpDoc extension that allows me to something like this:

/**
 * @route admin/test/
 */
public static function SomeMethod() { /* implementation */ }

But that would require writing/reusing a parser for phpDoc, and will most likely be rather slow.

The other option would be to separate each route into it’s own class, and have methods like the following:

class CAdminTest extends CRoute
{
    public static function Invoke() { /* implementation */ }
    public static function GetRoute() { return "admin/test/"; }
}

However, this would still require registering every single class, and there would be a great number of classes like this (not to mention the amount of extra code).

So what are my options here? What would be the best way to keep the route close to the method it invokes?

  • 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-13T01:31:44+00:00Added an answer on May 13, 2026 at 1:31 am

    This is how I ended up solving this. The article provided by Kevin was a huge help. By using ReflectionClass and ReflectionMethod::getDocComment, I can walk through the phpDoc comments very easily. A small regular expression finds any @route, and is registered to the method.

    Reflection is not that quick (in our case, about 2,5 times as slow as having hard-coded calls to RegiserRoute in a separate function), and since we have a lot of routes, we had to cache the finished list of routes in Memcached, so reflection is unnecessary on every page load. In total we ended up going from taking 7ms to register the routes to 1,7ms on average when cached (reflection on every page load used 18ms on average.

    The code to do this, which can be overridden in a subclass if you need manual registration, is as follows:

    public static function RegisterRoutes()
    {
        $sClass = get_called_class(); // unavailable in PHP < 5.3.0
        $rflClass = new ReflectionClass($sClass);
        foreach ($rflClass->getMethods() as $rflMethod)
        {
            $sComment = $rflMethod->getDocComment();
            if (preg_match_all('%^\s*\*\s*@route\s+(?P<route>/?(?:[a-z0-9]+/?)+)\s*$%im', $sComment, $result, PREG_PATTERN_ORDER)) 
            {
                foreach ($result[1] as $sRoute)
                {
                    $sMethod = $rflMethod->GetName();
                    $oRouteManager->RegisterRoute($sRoute, array($sClass, $sMethod));
                }
            }
        }
    }
    

    Thanks to everyone for pointing me in the right direction, there were lots of good suggestions here! We went with this approach simply because it allows us to keep the route close to the code it invokes:

    class CSomeRoutable extends CRoutable
    {
        /**
         * @route /foo/bar
         * @route /for/baz
         */
        public static function SomeRoute($SomeUnsafeParameter)
        {
            // this is accessible through two different routes
            echo (int)$SomeUnsafeParameter;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 228k
  • Answers 228k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer select u2.UserID, u2.EID, u2.Name, u2.Email, u2.Department, u2.Enabled, s.SiteID, s.SiteCode, s.SiteName… May 13, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer One way do get events is using the FQL. Here… May 13, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer If it were me, I would serialize the rows into… May 13, 2026 at 1:31 am

Related Questions

I was wondering if something exists (in Java world) able to take an snapshot
In Java, I will occasionally throw an AssertionError directly, to assert that a particular
I haven't done much Java development in about six years or so. I may
I'm a .NET web developer who has just been asked to produce a small
I converted my program from Delphi 4 to Delphi 2009 a year ago, mainly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.