469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

How to call base class function in sub class?

20
Scenario

Base Class A and its Sub Classes are B, C, D, E, F
Every class have a method B_Menu(), C_Menu(), D_Menu(), E_Menu(), F_Menu() respectively. And following Search_Form ($FileName,$Heading,$SubHeading) method with different x_Menu() method where x=B, C, D, E, F for every Sub Class mentioned below:
Expand|Select|Wrap|Line Numbers
  1.    <?php function Search_Form($FileName,$Heading,$SubHeading){     ?>
  2. <FORM method="POST" action="<?php echo $filename;?>">
  3. <table border="0" cellpadding="0" cellspacing="0" width="100%" id="AutoNumber1" height="344"><?php  $this->Page_Heading("$Heading"); ?>
  4. <tr> <td width="100%" height="51" colspan="2"><BR>
  5. <?php if($Heading=="REPORT")$this->B_Menu(); ?>&nbsp;
  6. <p align="center"><b><font size="4"> <u>SEARCH FOR<?php echo $SubHeading; ?></u></font></b></td></tr><tr><td width="50%" height="51"><strong>User's ID :</strong></td><td width="50%" height="51">
  7. <p align="center"><input type="text" name="PatID" size="10"></td></tr><tr>
  8. <td width="100%" height="19" colspan="2">
  9. ------------------------------------------------------OR-------------------------------------------------</td>
  10. </tr><tr><td width="50%" height="51"><strong>First Name :</strong></td>
  11. <td width="50%" height="51"><p align="center"><select size="1" name="FName">
  12. <option selected>Select First Name...</option></select></td></tr><tr>
  13. <td width="50%" height="51"><strong>Last Name : </strong></td>
  14. <td width="50%" height="51"><p align="center"><select size="1" name="LName">
  15. <option selected>Select Last Name...</option></select></td></tr></table>
  16. <p align="center"><?php  $this->Submit_Reset("  Search  ","  Reset  "); ?> </p></FORM><?php  }     
………..
Calling Search_Form ($FileName,$Heading,$SubHeading) method as follows:
Expand|Select|Wrap|Line Numbers
  1. ObjA-> Search_Form (‘x.php’,’REPORT’,’Update’);
  2. ObjB-> Search_Form (‘y.php’,’REPORT’,’Delete’);
:
:
:

Question?
If I move Search_Form ($FileName,$Heading,$SubHeading) method to Base Class A
What changes are required? and how can I call it in any Sub Class?
Apr 5 '10 #1
16 7941
chathura86
227 100+
pleas edit you code and set the [code] tags properly

any way from the description i got the following idea

you have a base class A

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     class ClassA
  3.     {
  4.         public function Search_Form ($FileName,$Heading,$SubHeading)
  5.         {
  6.             //do something here
  7.             echo $FileName . "<br>";
  8.         }
  9.     }
  10.  
  11.     class ClassB extends ClassA
  12.     {
  13.         public function B_Menu()
  14.         {
  15.  
  16.         }
  17.     }
  18.  
  19.     class ClassC extends ClassA
  20.     {
  21.         public function C_Menu()
  22.         {
  23.  
  24.         }
  25.     }
  26.  
  27.     class ClassD extends ClassA
  28.     {
  29.         public function D_Menu()
  30.         {
  31.  
  32.         }
  33.  
  34.         public function search()
  35.         {
  36.             $this->Search_Form('d1', 'd2', 'd3');
  37.         }
  38.     }
  39.  
  40.     $b = new ClassB();
  41.     $b->Search_Form('b1', 'b2', 'b3');
  42.  
  43.     $c = new ClassC();
  44.     $c->Search_Form('c1', 'c2', 'c3');
  45.  
  46.     $d = new ClassD();
  47.     $d->search();
  48. ?>
  49.  
this works because ClassB & ClassC are extending ClassA
so they can access the parent Class methods.

from a sub class $this can use to access parent class methods

Regards
Apr 5 '10 #2
iskhan
20
hi
May be I can't convey my issue Now another try
a search() define in every sub class and in the search() another function of sub class menu() is call like this:
Expand|Select|Wrap|Line Numbers
  1. search()
  2. {
  3. //screen design in html
  4. $this->menu();
  5. //this is the different line in every subclass because menu() is 
  6. //different in every sub class
  7. //screen design in html
  8. }
Because search() is repeat in all sub classes I want to move it on base class
so what are the changes required to move search() into base class and how to call search() in every subclass with different menu();
Apr 6 '10 #3
Dormilich
8,651 Expert Mod 8TB
there is no objection of moving search() in the base class (especially since it’s the same for every subclass)

you call it the same way: $this->search(). as long as there is no search() method in the current class, PHP looks it up in each ancestor class, until it finds the method.
Apr 6 '10 #4
iskhan
20
Yes you are right but how to use the menu() in search() because every subclass have menu() with different functionally.
e.g.
sub class A have 2 menu() which are menu1() & menu2()
sub class B have 3 menu() which are menu1() , menu2() & menu3()
sub class C have 1 menu() which is menu1()
Apr 7 '10 #5
chathura86
227 100+
could you please post the codes of the classes so i can analyse them properly
i really didn't get your question this time

Regards
Apr 7 '10 #6
Dormilich
8,651 Expert Mod 8TB
I can’t see menu() used in search().
Apr 7 '10 #7
iskhan
20
Expand|Select|Wrap|Line Numbers
  1. class main
  2. {
  3. //other base class functions
  4. }
  5. class A extend main
  6. {
  7.    menu1(){//Use in search()
  8. //screen design in html
  9.    }
  10.    menu2(){
  11. //screen design in html
  12.   }
  13.   search() {
  14. //screen design in html
  15.   $this->menu1();
  16. //this is the different line in every subclass because menu() is 
  17. //different in every sub class
  18. //screen design in html
  19.   }
  20. }
  21. class B extend main
  22. {
  23.    menu1(){
  24. //screen design in html
  25.    }
  26.    menu2(){//Use in search()
  27. //screen design in html
  28.   }
  29.    menu3(){
  30. //screen design in html
  31.   }
  32.   search() {
  33. //screen design in html
  34.   $this->menu2();
  35. //this is the different line in every subclass because menu() is 
  36. //different in every sub class
  37. //screen design in html
  38.   }
  39. }
  40.  
  41. class A extend main
  42. {
  43.    menu1(){//Use in search()
  44. //screen design in html
  45.    }
  46.   search() {
  47. //screen design in html
  48.   $this->menu1();
  49. //this is the different line in every subclass because menu() is 
  50. //different in every sub class
  51. //screen design in html
  52.   }
  53. }
  54.  
Apr 7 '10 #8
Dormilich
8,651 Expert Mod 8TB
you could work around that with an abstract class.
Expand|Select|Wrap|Line Numbers
  1. abstract class Main
  2. {
  3.     abstract public function menu();
  4.     public function search()
  5.     {
  6.         // some code with $this->menu()
  7.     }
  8. }
  9.  
  10. class A extends Main
  11. {
  12.     public function menu()
  13.     {
  14.         // code for menu in A
  15.     }
  16. }
  17.  
  18. $x = new A;
  19. $x->search();
Apr 7 '10 #9
iskhan
20
As I mention in sample code
In sub calss A menu1() call in search()
In sub calss B menu2() call in search()
In sub calss C menu1() call in search()

It means I define 2 or 3 abstract functions in base class like this:
abstract public function menu1();
abstract public function menu2();
.....
Am I right??
Apr 7 '10 #10
Dormilich
8,651 Expert Mod 8TB
if you make it a common name in each subclass, it’s way easier to handle than with different names. besides, since you define search() in Main, there is no implementation of search() in either subclass (you always use the same code).

of course you could do it this way, but that means you have to define menu1() and menu2() in each subclass (–> not worth it)
Apr 7 '10 #11
iskhan
20
Yes you are right but if functions are different like this:
Expand|Select|Wrap|Line Numbers
  1. class main
  2. {
  3. //other base class functions
  4. }
  5. class A extend main
  6. {
  7.    Customer(){//Use in Design()
  8. //screen design in html  
  9.  }
  10.   Design() {
  11. //screen design in html
  12.   $this->Customer(); //Only this is the different line in every subclass 
  13. //screen design in html
  14.   }
  15. }
  16. class B extend main
  17. {
  18.    Saller(){//Use in Design() 
  19. //screen design in html  
  20.   Design() {
  21. //screen design in html
  22.   $this->Saller();//Only this is the different line in every subclass 
  23. //screen design in html
  24.   }
  25. }
  26.  
  27. class C extend main
  28. {
  29.    Resaller(){//Use in Design() 
  30. //screen design in html  
  31.   Design() {
  32. //screen design in html
  33.   $this->Resaller();//Only this is the different line in every subclass 
  34. //screen design in html
  35.   }
  36. }
  37.  
Problem

Because every subclass have same function Design() but different function call of its class.
And Design() function repeat in every subclass so I need to move this funtion to Base class to avoid repeatation but If I move this funtion how can I call different funtions of every subclass in it??
i.e.
In class A I need to call Customer(); in it
In class B I need to call Saller(); in it
In class C I need to call Resaller(); in it
Apr 8 '10 #12
Dormilich
8,651 Expert Mod 8TB
then you can’t move it to the base class.

if Customer(), Seller() & Reseller() are doing different things (as the name suggests) there is (for me) no reason why Design() should be the same for each. there might be a totally different solution that suits your needs, but that depends on the whole picture.

if it’s just the call to these methods (and these methods don’t interact with the rest of the code) why don’t you define it this way:
Expand|Select|Wrap|Line Numbers
  1. class Main
  2. {
  3.     protected function searchMe()
  4.     {
  5.         // common code
  6.     }
  7. }
  8.  
  9. class A extends Main
  10. {
  11.     public function search()
  12.     {
  13.         $this->Customer();
  14.         $this->searchMe();
  15.     }
  16. }
Apr 8 '10 #13
Atli
5,058 Expert 4TB
I would probably do something like this
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. abstract class Main
  4. {
  5.     abstract protected function drawCustom();
  6.  
  7.     function Draw()
  8.     {
  9.         # Print HTML...
  10.  
  11.         $this->drawCustom();
  12.  
  13.         # Print more HTML
  14.     }
  15. }
  16.  
  17. class MainCustomer extends Main
  18. {
  19.     protected function drawCustom()
  20.     {
  21.         # Draw stuff for Customer
  22.     }
  23. }
  24. class MainSeller extends Main
  25. {
  26.     protected function drawCustom()
  27.     {
  28.         # Draw stuff for Seller
  29.     }
  30. }
  31. class MainReseller extends Main
  32. {
  33.     protected function drawCustom()
  34.     {
  35.         # Draw stuff for Reseller
  36.     }
  37. }
  38.  
  39. ?>
You could use the drawCustom() overwrite in each child class to do whatever you wanted. Leave it empty, even, if you don't need it.
Apr 8 '10 #14
iskhan
20
Thank I try both options.
Apr 8 '10 #15
Dormilich
8,651 Expert Mod 8TB
I’d try Atli’s option, since that got 2 votes.
Apr 8 '10 #16
iskhan
20
Thank you both of the Moderators to solve my problem you are the only two person who resolve my problem I send my problem to ten different groups and forums but no one solve it except you.
Thanks Again.
Apr 12 '10 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Fabian Müller | last post: by
9 posts views Thread by Daniel Kay | last post: by
3 posts views Thread by John A. Prejean | last post: by
20 posts views Thread by alexandre.braganti | last post: by
6 posts views Thread by roland.bali | last post: by
11 posts views Thread by dolphin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.