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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:59:51+00:00 2026-05-23T07:59:51+00:00

A little background: At runtime I would like to be able to inspect the

  • 0

A little background: At runtime I would like to be able to inspect the currently called functions javadoc-style documentation, to determine its formal (typed) declaration. This would allow runtime type checking (for simple and complex types) by means of reflection (at a cost) during debugging and testing, something that I would find immensely helpful.

So, in php I would like for a user defined function to get called whenever any other function is about to get called. That is, if a function foo() gets called, I would like to have my callHookHandler() function called immediately before.

One solution would be to implement __call() in all user defined classes, but that is both unwieldy and doesn’t include support for functions defined outside classes, so I am looking for a better solution.

  • 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-23T07:59:52+00:00Added an answer on May 23, 2026 at 7:59 am

    This sounds a bit of a fun one so I’m going to give answering it a try.

    I hope this helps you. Let me know how it goes.

    So, what you are asking can be done, and here’s how:

    For Functions:

    1. Get all defined functions with $function = get_defined_functions().
    2. Loop through the $functions['user'] key and inspect each one with the ReflectionFunction class. You’ll need to get:
    3. The comment using ->getDocComment()
    4. The arguments using ->getParameters()
    5. Do some magic (I’ll let you figure out how to parse the comment using some regular extressions and match it up with the parameter list from the reflection. Don’t forget optional parameters!)
    6. Rename the function using runkit_function_rename
    7. Generate code in a string that checks the parameters and calls the renamed function
    8. Generate a parameter list as a string
    9. Create a new function with runkit_function_add using the code you generated in step #5 and the parameter list from step #6.

    For Classes:

    1. Get a list of classes with $classes = get_declared_classes();
    2. Loop through each one and inspect it with ReflectionObject and ->getMethods() to get the methods. Make sure that the class is not internal with ->isInternal() because we can’t do anything about the internal classes.
    3. In an inner loop… go through each method using the ReflectionMethod class. Get the arguments and PHPDoc/JavaDoc comments just like you did with normal functions.
    4. Do the same thing you did with the functions only use runkit_method_add and runkit_method_rename instead.

    Downsides:

    You won’t be able to do the checking on internal class methods and functions (which is fine because they won’t have doc comments anyway).

    This is a lot of work! I left a lot of parts up to your imagination to avoid this being the length of a short book.

    Please send me this or open source it and let me know when you finish, I really want to use this myself. Contact info is on my website which is in my profile 😉

    Alternatively:

    You can use XDebug’s function trace along with reflection then analyze the results after the fact so that you don’t have to dynamically edit the code. If you want to write unit-test you could even automate it.

    Hope type checking makes it into future versions of PHP and wait: https://wiki.php.net/rfc/typechecking

    Notes:

    This class reference has a potentially useful example of parsing docComments in the comments section of the page:
    http://us.php.net/manual/en/class.reflectionmethod.php

    References

    • get_defined_functions
    • get_declared_classes
    • ReflectionFunction
    • ReflectionObject
    • ReflectionMethod
    • runkit
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

little background: currently putting together a website that is selling products, many of which
A little background first as to why I need this. I am currently creating
Little background: I have a JTextField called reading. It is located on a JFrame
A little background information: I have a table called table_a , which has 12
A little background: I'm creating a set of adapters to allow communication with mobile
A little background: I am loading a WPF UI from a database which is
A little background first. I've been tasked with encrypting files with a Powershell script
A little background first: I'm a designer/developer and decided to use subversion for a
A little background: I use PowerShell on windows xp at work and I set
For a little background, I work for a firm that develops web-based enterprise social

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.