• Resolved idono

    (@idono)


    Hi.

    I have a verry basic question about codefile inclusion when it comes to PHP.
    Is it common to only include extra files, like the facebook api, only when they are needed?

    Simply put i encountered a problem where another plugin is importing an earlier version of the Facebook API and i’m trying to use a newer version. I’ve tried exploring different venues but the only conclusion i can come to is to rewrite the plugin i’m using slightly to only load the facebook API when it needs to use it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Typically when working with things such as API’s it’s great to use things like function_exists() and class_exists() checks to allow future dev’s to overwrite these at a later date.

    This allows future developers to overwrite the file entirely, thus, upgrading the API or downgrading in some cases.

    With that being said, though I’ve never used this, you could look into override_function() and rename_function()

    http://stackoverflow.com/questions/15230883/how-to-override-built-in-php-functions

    Thread Starter idono

    (@idono)

    Thx for the input. I solved it in another way. I ended up changing some lines in facebooks autoloader.php file instead. I was on the right track from before i asked the question. Just got a bit blinded by frustration and forgot to change all the parts.

    Also i meant codefile inclusion in wordpress and not php. I’m meticulous about not including API files and constants when they aren’t needed. But that doesn’t seem to be all that common with the plugins i’m using. Wich is a bit worrying.

    For those that are interested. If you have dependency issues with the facebook API where the error states that it can’t find the class. It could be because someone is hogging the FACEBOOK_SDK_V4_SRD_DIR constant. So check if the constant has been defined. If it has then you can do 1 out of 3 things. Disable the plugin hogging the constant. But that’s hardly an option.

    In the second approach. Find this part in Facebooks autoload.php file.

    // For backwards compatibility
       $customBaseDir = '';
       //@todo v6: Remove support for 'FACEBOOK_SDK_V4_SRC_DIR'
       if (defined('FACEBOOK_SDK_V4_SRC_DIR')) {
            $customBaseDir = FACEBOOK_SDK_V4_SRC_DIR;
        } elseif (defined('FACEBOOK_SDK_SRC_DIR')) {
            $customBaseDir = FACEBOOK_SDK_SRC_DIR;
        }
        // base directory for the namespace prefix
        $baseDir = $customBaseDir ?: __DIR__ . '/';

    And change the FACEBOOK_SDK_V4_SRC_DIR and FACEBOOK_SDK_SRC_DIR constants to something that is guaranteed to be unique for your plugin.

    // For backwards compatibility
       $customBaseDir = '';
       //@todo v6: Remove support for 'FACEBOOK_SDK_V4_SRC_DIR'
       if (defined('FBAF_FACEBOOK_SDK_V4_SRC_DIR')) {
            $customBaseDir = FBAF_FACEBOOK_SDK_V4_SRC_DIR;
        } elseif (defined('FBAF_FACEBOOK_SDK_SRC_DIR')) {
            $customBaseDir = FBAF_FACEBOOK_SDK_SRC_DIR;
        }
        // base directory for the namespace prefix
        $baseDir = $customBaseDir ?: __DIR__ . '/';

    In my case i just prefixed the constants with FBAF_

    Third option is to comment out the entire if/else statement. Based on the todo tag. Support for the src dir constant will be removed completly in version 6 of the api.

    I strongly recommend option 2.

    Edit: I spoke to soon. But it fixes the problem of not finding some of the classes in V5.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘code file inclusion question.’ is closed to new replies.