0

I've a shared $sdk object reference (it's Facebook PHP SDK) between many objetcs. I need to save the access token at very beginning of a function call and restore after $this->sdk->api call. See for example getAlbums() function.

How can automatically execute a callback before/after every function call on every FBItem instance?

abstract class Item { protected $id, $sdk, $auth; public function __construct(Facebook $sdk, $auth = null) { $this->sdk = $sdk; $this->auth = $auth; } public function getAlbums() // Require access token change { // Am i FBUser or FBPage? Call setAccessToken to set auth $backup = $this->sdk->getAccessToken(); $this->sdk->setAccessToken($auth ?: $backup); $as = array(); $rs = $this->sdk->api(sprintf('/%s/albums', $this->id)); foreach($rs['data'] as $i) $as[] = new Album($this->sdk, $this->auth); // Restore previous token backup $this->sdk->setAccessToken($backup); } } class User extends Item { $ps = array(); $rs = $this->sdk->api(sprintf('/%s/accounts', $this->id)); foreach($rs['data'] as $i) $ps[] = new Page($this->sdk, $i['access_token']); return $ps; } class Page extends Item { } 

1 Answer 1

2

Probably the only way (without using php rootkit or rewriting whole classes) is preparing wrapper such as this:

class FBItemWrapper { public $item = new FBItem(); public function __call( $functionName, $args){ // Pre callback $result = call_user_func_array( array( $this->item, $functionName), $args); // Post callback return $result; } } 

You may set object dynamically so one Wrapper will be enough for everything.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.