SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple extends

    Hello,
    still coding under PHP 4.

    I have a few apps. There are some recursive methods. As my stuff is coded now, these methods are coded in each application. I would like to group them in one class and make this class the parent class of all the parent classes of my various applications.

    Like:
    PHP Code:
    class MainParent {

        
    //share all the methods common to all apps

    }

    class 
    Application1 extends MainParent {

        
    //this class is the parent class of all the Application1 classes and contains the methods used by all the Application1 classes.

    }

    class 
    Application2 extends MainParent {

        
    //this class is the parent class of all the Application2 classes  and contains the methods used by all the Application2 classes.


    I think that I'm good so far.

    Now to what refers parent:: in the application subclasses? or $this-> ?

    Like in:
    PHP Code:
    class DoSomething extends Application2 {

        function 
    DoSomething(){

            
    parent::sharedMethod();

        }

        function 
    DoSomethingElse(){

            
    $var $this->foo;

        } 


    thanks in advance

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $this will refer to the instance itself, i.e., a DoSomething object.

    parent refers to the parent class, i.e. Application2.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then how can reach the parent of the parent?

    parent:: parent::sharedMethod();

    ?

  4. #4
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $this will refer to the instance itself
    This is correct, but a subclass is a type of parent, so $this->sharedMethod() should work i.e.
    PHP Code:
    <?

    class basebase {
        function 
    speak()
        {
            print 
    "hello";
        }
    }

    class 
    base extends basebase { }

    class 
    child extends base {
        function 
    child()
        {
            
    $this->speak();
        }
    }

    $c = new child();

    ?>
    prints "hello"

  5. #5
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used this method for a calendar class; Calendar parent class which is extended by Day, Week and Month classes.

    It's worth noting that you will have some fun with include() and require() functions calls...

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    Then how can reach the parent of the parent?

    parent:: parent::sharedMethod();

    ?
    PHP Code:
    MainParent::sharedMethod(); 
    As Scheisskopf noted, you don't have to specify that the method is defined in a parent class unless you've overridden it in a subclass and need to specify that it's the base class version you want.

    I.e., unless you override sharedMethod() in Application2 and/or DoSomething, you can simply call it like this:
    PHP Code:
    $this->sharedMethod(); 
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies.
    I searched php.net for MainParent:: but couldn't find anything about it... Do you have some reference about it?


  8. #8
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's worth noting that you will have some fun with include() and require() functions calls...
    That's where the autoload function comes in handy.

    @jjshell: I suspect that MainParent is a made up class

  9. #9
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "MainParent" is the name of the parent parent's class.

    If you don't want to statically call it by name, you can just pass it up the chain too.

    PHP Code:
    <?php

    class MainParent 

        function 
    sharedMethod() {
            echo 
    'MainParent::sharedMethod()';
        }



    class 
    Application2 extends MainParent 

        function 
    sharedMethod() {
            
    parent::sharedMethod();
        }

    }


    class 
    DoSomething extends Application2 

        function 
    DoSomething() {
            
    parent::sharedMethod();
        }

        function 
    DoSomethingElse() {
            
    $var $this->foo;
        }

    }

    $doit = new DoSomething();
    outputs:
    Code:
    MainParent::sharedMethod()

  10. #10
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    Thanks for your replies.
    I searched php.net for MainParent:: but couldn't find anything about it... Do you have some reference about it?

    Quote Originally Posted by jjshell
    PHP Code:
    class MainParent 

        
    //share all the methods common to all apps 


    It's what you called your top-level base class.
    Birnam wood is come to Dunsinane

  11. #11
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Application" classes are almost certainly trying to do too much. I think you'll need to split this up into smaller, more focussed classes with discrete roles.

    Once you've encapsulated everything nicely you then need to figure out how to get everything talking to each other... Studying design patterns will help to give you some ideas. Inheritance is definitely not the way to do this and should be reserved for situations where the two classes are basically the same thing.

  12. #12
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What patterns could fit?


  13. #13
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it the same with variables? Are they all shared? I seem to have a hard time getting them...

    PHP Code:
    class MainParent 

      var 
    $var;

      function 
    MainParent()
      {

          
    $this->var 'foo';

      }




    class 
    Application extends MainParent 



    class 
    DoSomething extends Application 


        function 
    DoSomethingElse(){ 

            return 
    $this->var;

        } 


    the output of DoSomethingElse should be 'foo' right?

  14. #14
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You must explicitly call a parent constructor, eg:

    PHP Code:
    class DoSomething extends Application 
    {
        function 
    DoSomething(){
            
    $this->MainParent();
        }
        function 
    DoSomethingElse(){
            return 
    $this->var;
        }
        
    // etc 

  15. #15
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    What patterns could fit?

    OOP design takes a while to get to grips with. Patterns give you some ideas about how to weave objects together but you wouldn't code to fit a pattern: you fit the patterns to your requirements. The key point is to create objects with clearly defined responsibilities which don't try to do too much.

    If you're starting out with OOP you won't get it right first time - even experienced programmers won't get it right first time. It's a kind of evolutionary process. The forces of (un)natural selection are questions such as are the objects too tightly coupled? Are they cohesive? What happens if you stress the design with a certain type of change such as a switch to a different database? If you're testing, the bottom line is do the unit & etc tests run green?

    It's a strange mix of philosophy and engineering. You're free to invent your own entities to carry out particular roles although they also have to relate closely to practical requirements. You'll get killed in programming with any lack of clarity all the way from variable and method names right up to the top level client requirements and the first step is to identify exactly what your requirements are then code to these. Modern practices like Extreme Programming put a strong emphasis on clear requirements and use testing to stay focussed on these.

    http://c2.com/cgi/wiki?CouplingAndCohesion
    http://www.developerspot.com/tutoria...ent/page1.html

  16. #16
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    You must explicitly call a parent constructor
    Only if you override it with the subclass constructor. Otherwise the parent class contructor is called automatically -- jjshell's code works perfectly:

    PHP Code:
    class MainParent
    {
        var 
    $var;

        function 
    MainParent()
        {
            
    $this->var 'foo';
        }
    }


    class 
    Application extends MainParent
    {
    }

    class 
    DoSomething extends Application
    {
        function 
    DoSomethingElse(){
            return 
    $this->var;
        }
    }


    $test = new DoSomething();
    echo 
    $test->DoSomethingElse(); 

  17. #17
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops my mistake. I got mixed up with all those DoSomething's.

  18. #18
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    OOP design takes a while to get to grips with. Patterns give you some ideas about how to weave objects together but you wouldn't code to fit a pattern: you fit the patterns to your requirements. The key point is to create objects with clearly defined responsibilities which don't try to do too much.

    If you're starting out with OOP you won't get it right first time - even experienced programmers won't get it right first time. It's a kind of evolutionary process. The forces of (un)natural selection are questions such as are the objects too tightly coupled? Are they cohesive? What happens if you stress the design with a certain type of change such as a switch to a different database? If you're testing, the bottom line is do the unit & etc tests run green?

    It's a strange mix of philosophy and engineering. You're free to invent your own entities to carry out particular roles although they also have to relate closely to practical requirements. You'll get killed in programming with any lack of clarity all the way from variable and method names right up to the top level client requirements and the first step is to identify exactly what your requirements are then code to these. Modern practices like Extreme Programming put a strong emphasis on clear requirements and use testing to stay focussed on these.

    http://c2.com/cgi/wiki?CouplingAndCohesion
    http://www.developerspot.com/tutoria...ent/page1.html
    I'd second much of that .. if not all!

    I took quite a while to warm to object-oriented PHP.

    In fact, for a while, I was doin' it while not diggin' it!

    In the end, the immediate benefits were much tidier code, which meant more manageable structure, which meant saved time .. which meant saved money.

    Eventually, many more benefits come to fore...

  19. #19
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The mainparent class isn't instantiated... maybe that's why why I can't get it? It's like the Application class can use the vars set in MainParent but not DoSomething...
    Does anyone have an idea why? Any usual suspect around?

    Last edited by jjshell; Dec 16, 2005 at 02:39.

  20. #20
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The example you posted works exactly as it is. I pasted your code into an editor and added two lines:
    PHP Code:
    $foo = new DoSomething();
    echo 
    $foo->DoSomethingElse(); 
    The result, when I ran it, was:
    foo
    Birnam wood is come to Dunsinane

  21. #21
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok fine... Got it.

    Now please take a look at that scenario:

    PHP Code:
    //MainParent() is never instantiated 
    class MainParent 

      var 
    $var//var should be available to all the children 
      
    var $bar//bar should be available to all the children 

      
    function MainParent() 
      { 

          
    $this->bar 'bar'

      } 




    class 
    Application extends MainParent 

      var 
    $doSomething;
      
      function 
    Application() 
      { 

          
    $this->var 'foo'
          
    $this->doSomething = new DoSomething(); 
          
      }
      
      function 
    getVar()
      {
      
          return 
    $this->doSomething->returnVar();
      
      }
      
      function 
    getBar()
      {
      
          return 
    $this->doSomething->returnBar();
      
      } 
      
      
      



    class 
    DoSomething extends MainParent 

      function 
    DoSomething() 
      { 

          
    parent::MainParent(); 

      } 

      function 
    returnVar() 
      { 

            return 
    $this->var

      } 

      function 
    returnBar() 
      { 

            return 
    $this->bar

      } 



    $foo = new Application(); 
    echo 
    $foo->getVar(); 
    echo 
    $foo->getBar(); 
    I can get $bar but not $var... how come?

    thank you all so much for your support and patience by the way

  22. #22
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell
    I can get $bar but not $var... how come?
    Constructors sorta bubble up the inheritance hierarchy. The interpreter looks for a constructor in the instantiated class; if it doesn't find it there, it looks in its parent class, and so on. As soon as it finds one, the interpreter executes it and stops looking.

  23. #23
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to call the base class constructor in Application's constructor, since you supply an explicit constructor (base class constructors are only called implicitly if there is no explicit constructor in the derived class).

    PHP Code:
      function Application() 
      { 

          
    // call the base class constructor
          
    $this->MainParent();

          
    $this->var 'foo'
          
    $this->doSomething = new DoSomething(); 
           
      } 
    Birnam wood is come to Dunsinane

  24. #24
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great Tommy!

    thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •