Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've the following files:

`index.php`
`settings.php`
`functions.php`

settings.php:

....
$object = new SomePhpClass();
....

index.php:

require_once('settings.php');
require_once('functions.php');
....
some_function($object);
some_other_function($object);
....

functions.php:

function some_function(SomePhpClass $object){
   //Do something with $object 

}
function some_other_function(SomePhpClass $object){
   //Do some here
}

In my above code, I've passed the $object in the function. The above code works as of now. I want the functions.php to accept a wrapper class to SomePhpClass instead of directly passing the $object. I'm a newbie here and don't know how to go about writing the wrapper class to achieve such thing.

share|improve this question
Why do you want to wrap the object? – CodeTwice Jun 8 at 13:56
@CodeTwice: Actually, I have a lot of functions in functions.php and passing the $object in each one of them is not a good way. – ptokya Jun 8 at 13:58
1  
@ptokya What's the larger context? What are you trying to achieve? – Leri Jun 8 at 13:59
@PLB: I'm developing APIs for an application that uses a PHP Class. So by using the wrapper class method, the API can easily swap backend in future if other developers work on it. – ptokya Jun 8 at 14:00
@ptokya: Even if you wrap $object in some other class, you will still be passing in something, it just won't be $object, it will be $wrappedObject instead. Would it take you closer to whatever goal you are trying to achieve? – CodeTwice Jun 8 at 14:02
show 10 more comments

1 Answer

up vote 1 down vote accepted

Wrapper classes can be used for a lot of different reasons. Most of the time you use them to either hide the functionality of existing classes of yours so that the consumer only sees a limited interface (facade) or you can also use them to augment the functionality of an existing class or its method (decorator). Sometimes they are used as an adapter between different interfaces.

The basic idea of a wrapper class is that you define a class which contains the object you are wrapping (usually as private property) and defines the methods you want to make accessible. If your intention is to hide certain methods of your original object, the methods on your wrapper class will be a subset of your wrapped object's methods and will simply forward the call to the wrapped object and return whatever it returned. If you want to extend the object's functionality, you can also define new methods on your wrapper which do whatever magic you want them to do on your object.

A typical implementation could look something like this:

class SomePhpWrapper {
    /**
     * The wrapped object
     */
    private $wrapped;

    /**
     * Wrapping constructor
     */
    public function __construct(SomePhpClass $object) {
        $this->wrapped = $object;
    }

    /**
     * A method of the wrapped object we would like to explose on the wrapper
     */
    public function someMethod($someArgument) {
        return $this->wrapped->someMethod($someArgument);
    }

    /**
     * A decorated method which calls the wrapper class's method but also does something else
     */
    public function query($params) {
        $returnValue = $this->wrapper->query($params);
        doSomethingFunky();
        return $returnValue;
    }

    /**
     * Another exposed method
     */
    public function ping() {
        return $this->wrapped->ping()
    }

    /**
     * A decorator method that does not exist on the wrapped object but would be useful 
     */
    public function somethingComplicated() {
        $this->wrapped->query(this);
        $this->wrapped->query(that);
    }
}

This wrapper hides some methods of your original class and also add a new method called somethingComplicated(). This wrapper, however, is not a subclass of your original class. When you write functions that use this wrapper class, they should not expect SomePhpClass, they should expect SomePhpWrapper instead. In your case:

function some_function(SomePhpWrapper $object) {
    $object->ping(); // works
    $object->somethingComplicated(); // works
    $object->getDebug(); //fails, as getDebug() is not exposed by the wrapper
}

To use this wrapper you could go like this:

$object = new SomePhpClass();
$wrapper = new SomePhpWrapper($object);
some_function($wrapper);
share|improve this answer
Thanks @CodeTwice! – ptokya Jun 8 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.