SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __call() and function overloading

    Hello,
    here is my situation:
    some piece of code (it's about authorization) should be repeated in every method of class, serving webservice via Soap, I hoped magic method __call() helps me, but it doesn't call when requested method exists

    here what I supposed to code:
    PHP Code:
    class Foo {
        function 
    __call() {
            if (
    $m !== 'auth') {
                if (!isset(
    $this->user['oid'])) {
                    throw new 
    Exception('Not authorized');
                }
            }
            return 
    call_user_func_array(array($this$m), $p);
        }
    }
        function 
    bar() {
             
    /* do smth here only if you are authorized 
                 but I don't want to duplicate checking in every method
            */
        

    can you give me piece of advice on how to implement this functionality, may be any pattern I've missed or smth. similar

    Thank you

  2. #2
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __call() is a fallback for when a method doesn't exist, so it's not applicable quite in this fashion.

    One option you have is decorating the object with one that does the authorization.

  3. #3
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could rename the methods in some systematic way, so that, for example, a call to foo() generates a call to _foo() if authorized.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais


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
  •