SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 33 of 33
  1. #26
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greywire View Post
    It does not seem possible to do annotations on the parameters of a method
    or function (I don't see a getDocComment on the php parameter reflection
    class so I assume this is not possible). Is that true?
    Yes you are right. Annotations on parameters are not possible due to lack of getDocComment in reflection. But I am really curious about your usecase scenario for this one.

    Annotations on functions are not supported (yet). Well, i don't needed to annotate functions yet. ;-) Do you?
    Annotations support for PHP5
    TC/OPT™ Group Leader

  2. #27
    SitePoint Member
    Join Date
    Apr 2004
    Location
    california, usa
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johno View Post
    Yes you are right. Annotations on parameters are not possible due to lack of getDocComment in reflection. But I am really curious about your usecase scenario for this one.

    Annotations on functions are not supported (yet). Well, i don't needed to annotate functions yet. ;-) Do you?
    Well the idea is to auto generate a gui (or an XML/JSON service, or whatever) from the classes.

    So for example, you might have something like this:

    class User {
    function add(/* @unique */ $name,
    /* @password */ $password) {
    }
    }


    In this example, the @unique annotation might cause the gui to generate a form field with an ajax callback that tests for name in the database, and the @password annotation would create a password form field (where you cant see it being typed) and maybe even a verify field.
    --
    Trixbox CE Developer
    Fonality, inc.

  3. #28
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greywire View Post
    Well the idea is to auto generate a gui (or an XML/JSON service, or whatever) from the classes.

    So for example, you might have something like this:

    class User {
    function add(/* @unique */ $name,
    /* @password */ $password) {
    }
    }


    In this example, the @unique annotation might cause the gui to generate a form field with an ajax callback that tests for name in the database, and the @password annotation would create a password form field (where you cant see it being typed) and maybe even a verify field.

    I see. Maybe you could try something like this:

    Code:
    class Users {
      /*
       @Unique('user')
       @Password('pass')  
       */
      function add($user, $pass) { ... }
    }
    or exploit multiple annotations support
    Code:
    class Users {
      /*
       @Parameter('Unique')
       @Parameter('Password')  
       */
      function add($user, $pass) { ... }
    }
    Maybe this is even a first real world usecase for nested annotations (not supported, i am just speculating)

    Code:
    class Users {
      /*
       @Parameter(@Unique)
       @Parameter(@Password(minlength=8))  
       */
      function add($user, $pass) { ... }
    }
    Annotations support for PHP5
    TC/OPT™ Group Leader

  4. #29
    SitePoint Member
    Join Date
    Apr 2004
    Location
    california, usa
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or maybe something like this:

    Code:
    class Users {
      /*
      @parameter
      @unique
      */
      public $user;
      /*
      @parameter
      @password
      */
      public $pass;
    
      function add($user, $pass) { ... }
    
      function delete($user) {...}
    }
    The @parameter annotation would tell it that these properties are going to mirror the same named parameters in the methods. That way we save repeating ourselves, too..
    --
    Trixbox CE Developer
    Fonality, inc.

  5. #30
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, yes but you are somehow hacking object properties for something they were not designed for. I personally would prefer something like.

    Code:
    class User {
      /* @Unique */
      private $name;
    
      /* @HashedPassword("sha1") */
      private $password;
    }
    And then
    Code:
    class Users {
       public function add(User $user) { ... }
       public function delete(User $user) { ... }
    }
    Seems cleaner to me.
    Annotations support for PHP5
    TC/OPT™ Group Leader

  6. #31
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well done johno, and thanks for sharing the code!

  7. #32
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are welcome.

    Just share your experience with me if you will actually use it.
    Annotations support for PHP5
    TC/OPT™ Group Leader

  8. #33
    SitePoint Member
    Join Date
    Apr 2004
    Location
    california, usa
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    overthinking it..

    It occurs to me now that none of this is really necessary anyway. You just document the fact that the method arguments are the same as the properties. With proper naming (ie, dont name an argument the same as a property unless they really are the same) you can then just assume.

    To make use of the property annotations, you just pass the method parameters into the class properties, which would handle it just the same.

    In my case, I am unsetting the properties in the constructor, which then lets the __get/__set handle these, thus allowing the class to use the annotations to enforce things about the properties.
    --
    Trixbox CE Developer
    Fonality, inc.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •