473,583 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call base class function in sub class?

20 New Member
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,$Hea ding,$SubHeadin g) 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,$Hea ding,$SubHeadin g) 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,$Hea ding,$SubHeadin g) method to Base Class A
What changes are required? and how can I call it in any Sub Class?
Apr 5 '10 #1
16 8425
chathura86
227 New Member
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 New Member
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 Recognized Expert Moderator Expert
I can’t see menu() used in search().
Apr 7 '10 #7
iskhan
20 New Member
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 Recognized Expert Moderator Expert
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 New Member
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

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

Similar topics

23
5158
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
6506
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know...
9
3667
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 { /* ... */ }; template<class T> classSubject {
3
1528
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 and pass info to the form telling it which from to actually open. Any ideas how to do this? Here is an example of what I had in mind if the text...
20
4938
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 a VIRTUAL function createList(). This createList() function is overloaded in another class named ct_server that inherits gs_object. in my code,...
6
2481
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
3616
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 sending the Sandal type to the base class I can not instantiate a Sandal class from within Shoe. Public Class Shoe Public Shared Function Load() As...
4
14412
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 function from a base class reference: class A { public: virtual void function() = 0;
11
2855
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 void funcall( char *pch). if I pass a argument char* p1="fun1" .How do I call the function fun1() using that string "fun1"that I pass.
8
1683
by: ManicQin | last post by:
Hi lets say I have the next situation class base { pubic: ;) base() {} virtual ~base(){} virtual init() {//do init } };
0
7821
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7929
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5697
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3814
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1152
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.