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

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’);

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
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.     }
  11.     class ClassB extends ClassA
  12.     {
  13.         public function B_Menu()
  14.         {
  16.         }
  17.     }
  19.     class ClassC extends ClassA
  20.     {
  21.         public function C_Menu()
  22.         {
  24.         }
  25.     }
  27.     class ClassD extends ClassA
  28.     {
  29.         public function D_Menu()
  30.         {
  32.         }
  34.         public function search()
  35.         {
  36.             $this->Search_Form('d1', 'd2', 'd3');
  37.         }
  38.     }
  40.     $b = new ClassB();
  41.     $b->Search_Form('b1', 'b2', 'b3');
  43.     $c = new ClassC();
  44.     $c->Search_Form('c1', 'c2', 'c3');
  46.     $d = new ClassD();
  47.     $d->search();
  48. ?>
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

Apr 5 '10 #2
20 New Member
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
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
20 New Member
Yes you are right but how to use the menu() in search() because every subclass have menu() with different functionally.
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
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

Apr 7 '10 #6
8,658 Recognized Expert Moderator Expert
I can’t see menu() used in search().
Apr 7 '10 #7
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. }
  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. }
Apr 7 '10 #8
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. }
  10. class A extends Main
  11. {
  12.     public function menu()
  13.     {
  14.         // code for menu in A
  15.     }
  16. }
  18. $x = new A;
  19. $x->search();
Apr 7 '10 #9
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

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) .
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...
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 {
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...
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,...
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;
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...
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;
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.
by: ManicQin | last post by:
Hi lets say I have the next situation class base { pubic: ;) base() {} virtual ~base(){} virtual init() {//do init } };
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...
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. ...
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...
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...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.