Reflection API doesn't provide list of Nested Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rehevkor5

    Reflection API doesn't provide list of Nested Functions

    I am trying to use the reflection API in PHP 5 to execute the functions
    in a class which looks like:

    class Meow
    {
    function context()
    {
    function a()
    {
    }

    function b()
    {
    }
    }
    }


    I can run the following code to get the methods inside the Meow class,
    but have no way of getting the functions inside those methods. In this
    case, I specifically cannot get to a() and b() even though I know about
    context().

    $class = new ReflectionClass ('Meow');

    $methods = $class->getMethods() ;

    foreach($method s as $method)
    {
    //get functions inside $method (how??)
    }

    Is there a way to do this? If not, I'm thinking of submitting a bug
    report to PHP.net

  • Moot

    #2
    Re: Reflection API doesn't provide list of Nested Functions

    rehevkor5 wrote:
    I am trying to use the reflection API in PHP 5 to execute the functions
    in a class which looks like:
    >
    class Meow
    {
    function context()
    {
    function a()
    {
    }
    >
    function b()
    {
    }
    }
    }
    >
    >
    I can run the following code to get the methods inside the Meow class,
    but have no way of getting the functions inside those methods. In this
    case, I specifically cannot get to a() and b() even though I know about
    context().
    >
    $class = new ReflectionClass ('Meow');
    >
    $methods = $class->getMethods() ;
    >
    foreach($method s as $method)
    {
    //get functions inside $method (how??)
    }
    >
    Is there a way to do this? If not, I'm thinking of submitting a bug
    report to PHP.net
    Look at this page from the manual, specifically example 17-3.


    It seems as if the inner "bar" function does not exist until the
    function "foo" is called once. I'm not completely sure, but it would
    seem to me to be that functions a and b are not in the reflection API
    yet because as far as PHP knows, they don't exist.

    I'm curious, though, as to why you have structured the class this way.
    With only the code you provided to go on, I'd guess you're trying to
    change the logic for the a and b functions depending on which context
    function you call. A much cleaner way to do that would be to use the
    Factory pattern and subclass out each context you want to have
    available.

    Comment

    • rehevkor5

      #3
      Re: Reflection API doesn't provide list of Nested Functions

      Moot wrote:
      rehevkor5 wrote:
      I am trying to use the reflection API in PHP 5 to execute the functions
      in a class which looks like:

      class Meow
      {
      function context()
      {
      function a()
      {
      }

      function b()
      {
      }
      }
      }


      I can run the following code to get the methods inside the Meow class,
      but have no way of getting the functions inside those methods. In this
      case, I specifically cannot get to a() and b() even though I know about
      context().

      $class = new ReflectionClass ('Meow');

      $methods = $class->getMethods() ;

      foreach($method s as $method)
      {
      //get functions inside $method (how??)
      }

      Is there a way to do this? If not, I'm thinking of submitting a bug
      report to PHP.net
      >
      Look at this page from the manual, specifically example 17-3.

      >
      It seems as if the inner "bar" function does not exist until the
      function "foo" is called once. I'm not completely sure, but it would
      seem to me to be that functions a and b are not in the reflection API
      yet because as far as PHP knows, they don't exist.
      >
      I'm curious, though, as to why you have structured the class this way.
      With only the code you provided to go on, I'd guess you're trying to
      change the logic for the a and b functions depending on which context
      function you call. A much cleaner way to do that would be to use the
      Factory pattern and subclass out each context you want to have
      available.
      I bet you're right, good call. Now it's a question of how conscious a
      design decision that was. If I submit a bug report based on it, they
      may mark it "bogus" and say it behaves as expected.

      As for my code & why it's structured that way, it's just something I'm
      fiddling with to imitate the RSpec syntax. Since that's written in
      Ruby, it has some language constructs that don't really exist in PHP.
      I'm coming to the conclusion that it won't be possible to directly
      imitate RSpec. This is all just preliminary though. I thought I'd
      give it a whack. If my code did what you said, subclassing would
      indeed be much better.

      Comment

      Working...