Joseph U.
Joseph U.

Reputation: 4607

Proper way to access an object

I am working with the Facebook PHP library. It creates an object called $facebook.

In order to access the object within my functions I have to pass the object along with other parameters I want the function to process.

Is there a better way or is passing the object to the function an appropriate practice?

Upvotes: 0

Views: 69

Answers (3)

alex
alex

Reputation: 490263

Passing the variable is fine - it at least keeps you from defining $facebook as a global.

You could write a class for your functions, and pass the object in the constructor, e.g/

$myFacebook = new MyFacebook($facebook);

class MyFacebook {
    private $instance;
    public function __construct($facebookInstance) {
         $this->instance = $facebookInstance;
    }

}

Upvotes: 0

Jasper
Jasper

Reputation: 11908

Making the functions member functions of the facebook object. (though it won't be logicical for each and every function, it will make sense for quite some possible functions)

Funnily enough, the only thing that will actually change is that your object now becomes a "hidden" parameter instead of an actual one.

Upvotes: 0

fredley
fredley

Reputation: 33901

In your function definition: global $facebook;.

Upvotes: 1

Related Questions