简体   繁体   中英

php file having wrapper class instead of arguments in file's functions

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM