1

I am writing a function that calls another class and returns it so that I can execute my methods as $this->myUser()->getUsername()... now my question is:

Is there a way for me to intercept the ->getUsername() from inside myUser() or intercept the fail to find?...

This is because I want to use another class that is myUserExtended() and I was wondering if I could route them both together inside myUser(). so that if ->getCity() is not in myUser() it automaticaly goes on and uses another variable that containts myUserExtended().

Probably is not possible, but it is worth asking.

private function myUser( $setMyUser = false ) {

        if( $setMyUser ) {
            $this->_myUser = $setMyUser;
        }

        if( empty( $this->$_myUser ) ) {
            $this->_myUser = UserQuery::create()->findPK( $this->variables('userID') );
        }

        return $this->_myUser;
    }
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • 1
    with the [magic method](http://www.php.net/manual/en/language.oop5.overloading.php#object.call) `__call` ? – Brewal Jun 21 '13 at 14:07

3 Answers3

1

Something like this may do the trick:

public function __call($name, $args)
{
    if (function_exists($this->_myUser->$name())) {
        return $this->_myUser->$name();
    }
    return $this->_myUserExtended->$name();
}

You'll obviously have to adjust it if you are using any arguments.

Rijndael
  • 3,683
  • 2
  • 24
  • 26
  • What do you do with the arguments ? – Brewal Jun 21 '13 at 14:16
  • see http://www.php.net/manual/en/language.oop5.overloading.php#object.call - It has a few examples you can work with – Rijndael Jun 21 '13 at 14:17
  • I know, but I mean, if you call `$myObject->theMethod('arg1',21)`, if the method exists, will it call it first, or will it go to the `__call` ? – Brewal Jun 21 '13 at 14:20
  • I think you are wrong : "__call() is triggered when invoking inaccessible methods in an object context.". That means you never get into your `if` condition. You should just check if the method does not exists. – Brewal Jun 21 '13 at 14:23
  • see http://stackoverflow.com/questions/1071894/triggering-call-in-php-even-when-method-exists – Rijndael Jun 21 '13 at 14:24
  • @Rijndael no it will not go to `__call` - if a method exists in a class, then that method will be used. If a method **doesn't** exist in that class, then `__call` will be used – Ziarno Jun 21 '13 at 14:27
  • @Ziarno yes but what if it is private or protected ? What I meant at the begining, is that you should do this : `return call_user_func_array(array($this, $name), $args);` to keep arguments – Brewal Jun 21 '13 at 14:29
0

$this->myUser()->getUsername() is equivalent to $user = $this->myUser(); $user->getUsername(). If you can add methods to whatever class the $user variable contains, then you can use the __call() magic method and check if ->getCity() exists. Or you could just add a method called getCity() that calls through to myUserExtended().

Alex Grin
  • 8,121
  • 6
  • 33
  • 57
0

Maybe something like this will help:

class MyUserClass {
  private $_myUser;
  public function myUser( $setMyUser = false ) {

        if( $setMyUser ) {
            $this->_myUser = $setMyUser;
        }

        if( empty( $this->$_myUser ) ) {
            $this->_myUser = UserQuery::create()->findPK( $this->variables('userID') );
        }

        return $this; //if you want to use method chaining, you have to return $this
    }
  public function __call($name, $arguments) {
    $ext = new myUserExtended();
    $ext->$name($arguments);
    return $ext;
  }
}

$myUser = new MyUserClass();

$myUser->myUser()->getCity("New York"); //this will call $ext->getCity(array("New York"))
Ziarno
  • 7,366
  • 5
  • 34
  • 40