PHP extending class

I have a question about class extension in PHP.

The examples I see on the php site have only one line of code in the method ... this is the same if the method has tons of code

If it is a base class:

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

Then I need to copy all the code 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?? } } 

It seems a little dry to me ...

+4
source share
6 answers

Yes, if these 1 or 2 things are not at the beginning or at the end. You can call the parent function via

 parent::WithWayTooMuchCode(); 

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

If it does not feel DRY, consider splitting the function into smaller methods.

+4
source

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

No, you do not need to copy all the code, assuming you are adding a function and not deleting it.

so that:

 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(); 
+4
source

You can use the parent :: WithWayTooMuchCode (), which will execute the parent method even 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?? } } 
+2
source

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

In other words, highlight what you need to do separately and create a function for them. And name them separately in the class of children. The last function in the child class will call the function of the parent class, as well as the individual functions.

+1
source

You have several options. 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 "new code" and not "100 lines".

Finally, you could do

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

which allows you to run MyOwnClass-> WithWayTooMuchCode () will run "100 lines of code" and new code. You can place the parent before / after / during your new code so that you can adapt it as needed.

+1
source

In addition to the other answers, I must point out that this problem can be solved by events. This is a way to define points in a class where you can add your own functions from outside. If you have control over the code base and time / tilt, you can consider implementing this function. Unfortunately, PHP does not support them directly, since, for example, C #, you need to execute.

If you have this problem in only one place, I doubt that you should work hard, but if it becomes a template, you can consider this approach.

0
source

Source: https://habr.com/ru/post/1412016/


All Articles