I am working with the Facebook PHP library. It cre开发者_运维问答ates 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?
In your function definition: global $facebook;
.
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.
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;
}
}
精彩评论