473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8411
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,658 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,658 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,658 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,658 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,658 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,658 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

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
3
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
4
by: Javier | last post by:
Hello, is this possible? I have a pure virtual function in the base class (to force the programmers of the derived classes to have this function implemented) but I want to call the derived class...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
8
by: ManicQin | last post by:
Hi lets say I have the next situation class base { pubic: ;) base() {} virtual ~base(){} virtual init() {//do init } };
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.