Note that there are some explanatory texts on larger screens.

plurals
  1. POmulti-level inheritance substitution
    text
    copied!<p>I want to write a module (framework specific), that would wrap and extend Facebook PHP-sdk (<a href="https://github.com/facebook/php-sdk/" rel="nofollow noreferrer">https://github.com/facebook/php-sdk/</a>). My problem is - how to organize classes, in a nice way.</p> <p>So getting into details - Facebook PHP-sdk consists of two classes:</p> <ul> <li>BaseFacebook - abstract class with all the stuff sdk does</li> <li>Facebook - extends BaseFacebook, and implements parent abstract persistance-related methods with default session usage</li> </ul> <p>Now I have some functionality to add:</p> <ul> <li>Facebook class substitution, integrated with framework session class</li> <li>shorthand methods, that run api calls, I use mostly (through BaseFacebook::api()),</li> <li>authorization methods, so i don't have to rewrite this logic every time,</li> <li>configuration, sucked up from framework classes, insted of passed as params</li> <li>caching, integrated with framework cache module</li> </ul> <p>I know something has gone very wrong, because I have too much inheritance that doesn't look very normal. Wrapping everything in one "complex extension" class also seems too much. I think I should have few working togheter classes - but i get into problems like: if cache class doesn't really extend and override BaseFacebook::api() method - shorthand and authentication classes won't be able to use the caching. </p> <p>Maybe some kind of a pattern would be right in here? How would you organize these classes and their dependencies?</p> <p><strong>EDIT 04.07.2012</strong></p> <p>Bits of code, related to the topic:</p> <p>This is how the base class of Facebook PHP-sdk:</p> <pre><code>abstract class BaseFacebook { // ... some methods public function api(/* polymorphic */) { // ... method, that makes api calls } public function getUser() { // ... tries to get user id from session } // ... other methods abstract protected function setPersistentData($key, $value); abstract protected function getPersistentData($key, $default = false); // ... few more abstract methods } </code></pre> <p>Normaly Facebook class extends it, and impelements those abstract methods. I replaced it with my substitude - Facebook_Session class:</p> <pre><code>class Facebook_Session extends BaseFacebook { protected function setPersistentData($key, $value) { // ... method body } protected function getPersistentData($key, $default = false) { // ... method body } // ... implementation of other abstract functions from BaseFacebook } </code></pre> <p>Ok, then I extend this more with shorthand methods and configuration variables:</p> <pre><code>class Facebook_Custom extends Facebook_Session { public function __construct() { // ... call parent's constructor with parameters from framework config } public function api_batch() { // ... a wrapper for parent's api() method return $this-&gt;api('/?batch=' . json_encode($calls), 'POST'); } public function redirect_to_auth_dialog() { // method body } // ... more methods like this, for common queries / authorization } </code></pre> <p>I'm not sure, if this isn't too much for a single class ( authorization / shorthand methods / configuration). Then there comes another extending layer - cache:</p> <pre><code>class Facebook_Cache extends Facebook_Custom { public function api() { $cache_file_identifier = $this-&gt;getUser(); if(/* cache_file_identifier is not null and found a valid file with cached query result */) { // return the result } else { try { // call Facebook_Custom::api, cache and return the result } catch(FacebookApiException $e) { // if Access Token is expired force refreshing it parent::redirect_to_auth_dialog(); } } } // .. some other stuff related to caching } </code></pre> <p>Now this pretty much works. New instance of Facebook_Cache gives me all the functionality. Shorthand methods from Facebook_Custom use caching, because Facebook_Cache overwrited api() method. But here is what is bothering me:</p> <ul> <li>I think it's too much inheritance. </li> <li>It's all very tight coupled - like look how i had to specify 'Facebook_Custom::api' instead of 'parent:api', to avoid api() method loop on Facebook_Cache class extending.</li> <li>Overall mess and ugliness.</li> </ul> <p>So again, this works but I'm just asking about patterns / ways of doing this in a cleaner and smarter way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload