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: - <?php function Search_Form($FileName,$Heading,$SubHeading){ ?>
-
<FORM method="POST" action="<?php echo $filename;?>">
-
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="AutoNumber1" height="344"><?php $this->Page_Heading("$Heading"); ?>
-
<tr> <td width="100%" height="51" colspan="2"><BR>
- <?php if($Heading=="REPORT")$this->B_Menu(); ?>
-
<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">
-
<p align="center"><input type="text" name="PatID" size="10"></td></tr><tr>
-
<td width="100%" height="19" colspan="2">
-
------------------------------------------------------OR-------------------------------------------------</td>
-
</tr><tr><td width="50%" height="51"><strong>First Name :</strong></td>
-
<td width="50%" height="51"><p align="center"><select size="1" name="FName">
-
<option selected>Select First Name...</option></select></td></tr><tr>
-
<td width="50%" height="51"><strong>Last Name : </strong></td>
-
<td width="50%" height="51"><p align="center"><select size="1" name="LName">
-
<option selected>Select Last Name...</option></select></td></tr></table>
-
<p align="center"><?php $this->Submit_Reset(" Search "," Reset "); ?> </p></FORM><?php }
………..
Calling Search_Form ($FileName,$Heading,$SubHeading) method as follows: - ObjA-> Search_Form (‘x.php’,’REPORT’,’Update’);
-
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?
16 8411
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 -
<?php
-
class ClassA
-
{
-
public function Search_Form ($FileName,$Heading,$SubHeading)
-
{
-
//do something here
-
echo $FileName . "<br>";
-
}
-
}
-
-
class ClassB extends ClassA
-
{
-
public function B_Menu()
-
{
-
-
}
-
}
-
-
class ClassC extends ClassA
-
{
-
public function C_Menu()
-
{
-
-
}
-
}
-
-
class ClassD extends ClassA
-
{
-
public function D_Menu()
-
{
-
-
}
-
-
public function search()
-
{
-
$this->Search_Form('d1', 'd2', 'd3');
-
}
-
}
-
-
$b = new ClassB();
-
$b->Search_Form('b1', 'b2', 'b3');
-
-
$c = new ClassC();
-
$c->Search_Form('c1', 'c2', 'c3');
-
-
$d = new ClassD();
-
$d->search();
-
?>
-
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
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: - search()
-
{
-
//screen design in html
-
$this->menu();
-
//this is the different line in every subclass because menu() is
-
//different in every sub class
-
//screen design in html
-
}
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();
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.
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()
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
I can’t see menu() used in search().
-
class main
-
{
-
//other base class functions
-
}
-
class A extend main
-
{
-
menu1(){//Use in search()
-
//screen design in html
-
}
-
menu2(){
-
//screen design in html
-
}
-
search() {
-
//screen design in html
-
$this->menu1();
-
//this is the different line in every subclass because menu() is
-
//different in every sub class
-
//screen design in html
-
}
-
}
-
class B extend main
-
{
-
menu1(){
-
//screen design in html
-
}
-
menu2(){//Use in search()
-
//screen design in html
-
}
-
menu3(){
-
//screen design in html
-
}
-
search() {
-
//screen design in html
-
$this->menu2();
-
//this is the different line in every subclass because menu() is
-
//different in every sub class
-
//screen design in html
-
}
-
}
-
-
class A extend main
-
{
-
menu1(){//Use in search()
-
//screen design in html
-
}
-
search() {
-
//screen design in html
-
$this->menu1();
-
//this is the different line in every subclass because menu() is
-
//different in every sub class
-
//screen design in html
-
}
-
}
-
you could work around that with an abstract class. - abstract class Main
-
{
-
abstract public function menu();
-
public function search()
-
{
-
// some code with $this->menu()
-
}
-
}
-
-
class A extends Main
-
{
-
public function menu()
-
{
-
// code for menu in A
-
}
-
}
-
-
$x = new A;
-
$x->search();
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??
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)
Yes you are right but if functions are different like this: - class main
-
{
-
//other base class functions
-
}
-
class A extend main
-
{
-
Customer(){//Use in Design()
-
//screen design in html
-
}
-
Design() {
-
//screen design in html
-
$this->Customer(); //Only this is the different line in every subclass
-
//screen design in html
-
}
-
}
-
class B extend main
-
{
-
Saller(){//Use in Design()
-
//screen design in html
-
Design() {
-
//screen design in html
-
$this->Saller();//Only this is the different line in every subclass
-
//screen design in html
-
}
-
}
-
-
class C extend main
-
{
-
Resaller(){//Use in Design()
-
//screen design in html
-
Design() {
-
//screen design in html
-
$this->Resaller();//Only this is the different line in every subclass
-
//screen design in html
-
}
-
}
-
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
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: - class Main
-
{
-
protected function searchMe()
-
{
-
// common code
-
}
-
}
-
-
class A extends Main
-
{
-
public function search()
-
{
-
$this->Customer();
-
$this->searchMe();
-
}
-
}
Atli 5,058
Expert 4TB
I would probably do something like this - <?php
-
-
abstract class Main
-
{
-
abstract protected function drawCustom();
-
-
function Draw()
-
{
-
# Print HTML...
-
-
$this->drawCustom();
-
-
# Print more HTML
-
}
-
}
-
-
class MainCustomer extends Main
-
{
-
protected function drawCustom()
-
{
-
# Draw stuff for Customer
-
}
-
}
-
class MainSeller extends Main
-
{
-
protected function drawCustom()
-
{
-
# Draw stuff for Seller
-
}
-
}
-
class MainReseller extends Main
-
{
-
protected function drawCustom()
-
{
-
# Draw stuff for Reseller
-
}
-
}
-
-
?>
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.
Thank I try both options.
I’d try Atli’s option, since that got 2 votes.
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.
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. ...
|
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 {
/* ... */
};
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ManicQin |
last post by:
Hi lets say I have the next situation
class base
{
pubic: ;)
base() {}
virtual ~base(){}
virtual init()
{//do init }
};
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |