olly
olly

Reputation: 61

oop, php, extend class, run function when specific method in class was called

first of all, I'm pretty much a noobie regarding oop etc, so please be kind.

I did read a lot of manuals but somehow got lost in all the stuff and couldn't find the answers :( so any hints, links etc would be very much appreciated

anyway I find myself in a situation where I need to call a function when a specific method in a class is being executed. However, I cannot touch the class itself.

to demonstrate, the class is something like this:

/* icannot touch this class*/
class Bar extends Foo {
/*some other methods and variables here, omitted for sanity reasons**/

function DBSave(){      

    $this->cat_parent = intval($this->cat_parent);
    $this->cat_num_files = intval($this->cat_num_files);

    return parent::DBSave();
}

/*some more methods omitted for sanity reasons**/
}
/*end of class*/

/**edit***/

based on the comment below now I wrote/added the following but it doesnt really do things the way I had hoped :(

class Baz extends Bar {
    public function DBSave() {
        /*here I want to use $this->cat_parent etc to do something with*/
        parent::DBSave();
    }
}
/*this is probably also wrong, but I tried with the following results*/
$b = new Baz();/*just adding this does nothing*/
$b->DBSave();/* when this is added as well it *always* runs regrdless of whether bar->foo is called*/

also - I forgot to mention, sorry - the method in Baz needs the variables ($this->cat_parent etc) to something sensible with hope the above makes sense ...?

/**edit end***/

whenever DBSave gets called, I want to run another function but I have no idea how to go about that :( I tried a few things with extending the class but it just goes wrong or calls it all the time etc....any hints would be greatly appreciated

of course happy to expand on the issue if needed

thanks

Upvotes: 0

Views: 150

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

The simplest way would probably be to do:

class Baz extends Bar {
    public function DBSave() {
        parent::DBSave();
    }
}

You can define additional functionality in the body of that method.

Upvotes: 1

Related Questions