How to call base class function in sub class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iskhan
    New Member
    • Apr 2010
    • 20

    How to call base class function in sub class?

    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:
    Code:
       [B]<?php function Search_Form($FileName,$Heading,$SubHeading){     ?>[/B]
    <FORM method="POST" action="[B]<?php echo $filename;?>[/B]">
    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="AutoNumber1" height="344">[B]<?php  $this->Page_Heading("$Heading"); ?>[/B]
    <tr> <td width="100%" height="51" colspan="2"><BR>
    [B]<?php if($Heading=="REPORT")$this->B_Menu(); ?>[/B]&nbsp;
    <p align="center"><b><font size="4"> <u>SEARCH FOR[B]<?php echo $SubHeading; ?>[/B]</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">[B]<?php  $this->Submit_Reset("  Search  ","  Reset  "); ?>[/B] </p></FORM>[B]<?php  }[/B]
    ………..
    Calling Search_Form ($FileName,$Hea ding,$SubHeadin g) method as follows:
    Code:
    ObjA-> Search_Form (‘x.php’,’REPORT’,’Update’);
    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?
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    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

    Code:
    <?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

    Comment

    • iskhan
      New Member
      • Apr 2010
      • 20

      #3
      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:
      Code:
      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();
      Last edited by Dormilich; Apr 6 '10, 12:18 PM. Reason: Please use [code] tags when posting code

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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.

        Comment

        • iskhan
          New Member
          • Apr 2010
          • 20

          #5
          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()

          Comment

          • chathura86
            New Member
            • May 2007
            • 227

            #6
            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

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              I can’t see menu() used in search().

              Comment

              • iskhan
                New Member
                • Apr 2010
                • 20

                #8
                Code:
                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
                  }
                }

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  you could work around that with an abstract class.
                  Code:
                  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();

                  Comment

                  • iskhan
                    New Member
                    • Apr 2010
                    • 20

                    #10
                    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??

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      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)

                      Comment

                      • iskhan
                        New Member
                        • Apr 2010
                        • 20

                        #12
                        Yes you are right but if functions are different like this:
                        Code:
                        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

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          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:
                          Code:
                          class Main
                          {
                              protected function searchMe()
                              {
                                  // common code
                              }
                          }
                          
                          class A extends Main
                          {
                              public function search()
                              {
                                  $this->Customer();
                                  $this->searchMe();
                              }
                          }

                          Comment

                          • Atli
                            Recognized Expert Expert
                            • Nov 2006
                            • 5062

                            #14
                            I would probably do something like this
                            [code=php]<?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
                            }
                            }

                            ?>[/code]
                            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.

                            Comment

                            • iskhan
                              New Member
                              • Apr 2010
                              • 20

                              #15
                              Thank I try both options.

                              Comment

                              Working...