redconservatory
redconservatory

Reputation: 21934

PHP extending a class

I have a question about extending a class in PHP.

The examples I see on the php site just have 1 line of code in the method...is it the same if the method has tons of code??

If this is the base class:

class BaseClass {

    public function WithWayTooMuchCode {
      // like 100 lines of code here
    }

}

Then do I have to copy all the code over if I want to use the same method, but change only 1 or 2 things?

class MyOwnClass extends BaseClass {
     public function WithWayTooMuchCode {
      // like 100 lines of code here

     // do I have to copy all of the other code and then add my code??
    }

}

This seems a little not DRY to me...

Upvotes: 5

Views: 120

Answers (6)

Kristian
Kristian

Reputation: 21840

do I have to copy all the code over if I want to use the same method, but change only 1 or 2 things?

No, you don't have to copy all of the code, assuming you're adding to the function and not removing pieces of it.

so it follows:

class BaseClass {

    public function WithWayTooMuchCode {
      // like 100 lines of code here
    }

}

class MyOwnClass extends BaseClass {

    public function WithWayTooMuchCode { 
        parent::WithWayTooMuchCode();
        //additionally, do something else
    }

}

$moc = new MyOwnClass();
$moc->WithWayTooMuchCode();

Upvotes: 4

Manos Dilaverakis
Manos Dilaverakis

Reputation: 5879

In addition to the other answers I should point out that this problem can be addressed by events. They are a way to determine points in a class where you can add your own functionality from outside. If you have control over the codebase and the time/inclination you might want to consider implementing this functionality. Unfortunately PHP doesn't support them directly in the way that, say, C# does so you'd have to do the work.

If you only have this issue in a single place I doubt you should bother, but if it's becoming a pattern you might want to consider this approach.

Upvotes: 0

Laurence
Laurence

Reputation: 60068

You have a few choices. Assuming;

class BaseClass {

    public function WithWayTooMuchCode {
      // like 100 lines of code here
       }
    }

You can do

class MyOwnClass extends BaseClass {
     public function AnotherFunction() {
         // put other code here
    }

}

This allows you to do MyOwnClass->AnotherFunction() and MyOwnClass->WithWayTooMuchCode()

or you could do

class MyOwnClass extends BaseClass {
     public function WithWayTooMuchCode() {
         // put new code here
    }

}

which will allow you to run MyOwnClass->WithWayTooMuchCode() and will ONLY run the "new code", not the "100 lines".

Finally you could do

class MyOwnClass extends BaseClass {
     public function WithWayTooMuchCode() {
         parent::WithWayTooMuchCode();
         // Do more processing
    }

}

which will allow you to run MyOwnClass->WithWayTooMuchCode() will run the "100 lines of code" AND the new code. You can put the parent before/after/during your new code, so you can tailor it as required

Upvotes: 1

Rajan Rawal
Rajan Rawal

Reputation: 6313

You can write down a separate function for that things that you want to do separately in parent class and then Call then in you way.

In other words, separate out the things you need to do separately ans create a function for them. And call them separately in child class. The final function in child class will call parent class function as well those separate functions.

Upvotes: 1

Userbn
Userbn

Reputation: 342

You can use the parent::WithWayTooMuchCode() wich will execute the parent method and after you can add your code. It will look like this :

class MyOwnClass extends BaseClass {
     public function WithWayTooMuchCode {
      parent::WithWayTooMuchCode()

     // do I have to copy all of the other code and then add my code??
    }

}

Upvotes: 2

mpen
mpen

Reputation: 283325

Yes, unless those 1 or 2 things happen to be at the beginning or the end. You can call the parent function via

parent::WithWayTooMuchCode();

Which you can place anywhere in the child/overridden method.

If it doesn't feel DRY, consider splitting the function into smaller methods.

Upvotes: 5

Related Questions