Child construct parent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reandeau

    Child construct parent

    I'm building out a OO based app in PHP 5 but I'm getting a little
    confused on children contructing parents.

    I have a parent that looks like this:
    abstract Class State {
    protected $database;
    protected $user;
    protected $output;
    public function __construct($da tabase,$user,$o utput) {
    $this->database = $database;
    $this->user = $user;
    $this->output = $output;
    }
    }

    And a child that looks like this:
    Class Status extends State {
    public function __construct($da tabase,$user,$o utput) {
    parent::__const ruct($database, $user,$output);
    }
    }


    This seems to work OK. But I'm getting confused when thinking about
    adding a new child who then has to contruct the parent again. Isn't
    this getting away from the whole purpose of inheritence? It seems like
    this is recreating the parent with every new child that come along.
    Would it be possible to just have a single instance of the parent that
    all children extended or am I missing the point here?


    Thanks

    Jon

  • Andy Hassall

    #2
    Re: Child construct parent

    On 16 Aug 2006 14:38:57 -0700, "reandeau" <jon.tjemsland@ gmail.comwrote:
    >I'm building out a OO based app in PHP 5 but I'm getting a little
    >confused on children contructing parents.
    >
    >I have a parent that looks like this:
    >abstract Class State {
    > protected $database;
    > protected $user;
    > protected $output;
    > public function __construct($da tabase,$user,$o utput) {
    > $this->database = $database;
    > $this->user = $user;
    > $this->output = $output;
    > }
    >}
    >
    >And a child that looks like this:
    >Class Status extends State {
    > public function __construct($da tabase,$user,$o utput) {
    > parent::__const ruct($database, $user,$output);
    > }
    >}
    >
    >This seems to work OK. But I'm getting confused when thinking about
    >adding a new child who then has to contruct the parent again. Isn't
    >this getting away from the whole purpose of inheritence? It seems like
    >this is recreating the parent with every new child that come along.
    >Would it be possible to just have a single instance of the parent that
    >all children extended or am I missing the point here?
    You are possibly confusing "is-a" relationships between base class and
    subclass with a "has-a" relationship.

    Since Status "is a" State, then you need to do all the construction for the
    base class attributes first, then the construction for the subclass.

    There is no parent instance here - when you create a Status object, there is
    only one object; a Status object, which inherits everything that a State object
    has and can do, which then adds or overrides data and methods with data and
    methods deinfed in the State class.

    Now, if there is a common resource you want shared amongst all instances of
    the class, then that would be a "has-a" relationship - a variable in each class
    instance that is a reference to one separate instance of another object. A
    database connection might fall into this sort of category.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Dikkie Dik

      #3
      Re: Child construct parent

      ....
      And a child that looks like this:
      Class Status extends State {
      public function __construct($da tabase,$user,$o utput) {
      parent::__const ruct($database, $user,$output);
      }
      }
      >
      >
      This seems to work OK. But I'm getting confused when thinking about
      adding a new child who then has to contruct the parent again. Isn't
      this getting away from the whole purpose of inheritence? It seems like
      this is recreating the parent with every new child that come along.
      Would it be possible to just have a single instance of the parent that
      all children extended or am I missing the point here?
      You are not constructing the parent as a separate object. There is only
      one object that is specifically the child and more generally the parent
      (because the child is a specialization of the parent). However, the
      parent may need some data to be constructed with, even if it is
      abstract. Calling the parent's constructor from the child does just
      that: initialize the code in the parent class. Mind you, due to
      inheritance, that code is now active in the child object.

      An example:

      class Exception
      {private internalmessage ;
      public function __construct(mes sage)
      {this->internalmessag e = message;}
      public function getMessage()
      {return this->internalmessag e;}
      }

      class LoginException extends Exception
      {public function __construct(mes sage)
      {parent::__cons truct('Could not verify credentials.'); }
      }

      An exception always has to have a message, so it enforces one by
      requiring it in the constructor's parameters. This message is kept
      safely inside, so it is effectively read-only. Not even a subclass can
      touch it.
      The subclass LoginException sets an appropriate message by calling its
      parent's constructor (that is not done automatically by PHP). But as the
      internalmessage in the superclass is private, it is not within reach of
      the subclass. Off course, an instance of LoginException will have this
      variable somewhere in memory, but only the methods defined in the
      superclass can touch it. So there is just one instance then: that of the
      child.

      Best regards

      Comment

      • reandeau

        #4
        Re: Child construct parent


        Andy Hassall wrote:
        On 16 Aug 2006 14:38:57 -0700, "reandeau" <jon.tjemsland@ gmail.comwrote:
        >
        I'm building out a OO based app in PHP 5 but I'm getting a little
        confused on children contructing parents.

        I have a parent that looks like this:
        abstract Class State {
        protected $database;
        protected $user;
        protected $output;
        public function __construct($da tabase,$user,$o utput) {
        $this->database = $database;
        $this->user = $user;
        $this->output = $output;
        }
        }

        And a child that looks like this:
        Class Status extends State {
        public function __construct($da tabase,$user,$o utput) {
        parent::__const ruct($database, $user,$output);
        }
        }

        This seems to work OK. But I'm getting confused when thinking about
        adding a new child who then has to contruct the parent again. Isn't
        this getting away from the whole purpose of inheritence? It seems like
        this is recreating the parent with every new child that come along.
        Would it be possible to just have a single instance of the parent that
        all children extended or am I missing the point here?
        >
        You are possibly confusing "is-a" relationships between base class and
        subclass with a "has-a" relationship.
        >
        Since Status "is a" State, then you need to do all the construction for the
        base class attributes first, then the construction for the subclass.
        >
        There is no parent instance here - when you create a Status object, there is
        only one object; a Status object, which inherits everything that a State object
        has and can do, which then adds or overrides data and methods with data and
        methods deinfed in the State class.
        >
        Now, if there is a common resource you want shared amongst all instances of
        the class, then that would be a "has-a" relationship - a variable in each class
        instance that is a reference to one separate instance of another object. A
        database connection might fall into this sort of category.
        >
        --
        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
        Thanks for the help. That makes sense.

        Comment

        • Jerry Stuckle

          #5
          Re: Child construct parent

          reandeau wrote:
          I'm building out a OO based app in PHP 5 but I'm getting a little
          confused on children contructing parents.
          >
          I have a parent that looks like this:
          abstract Class State {
          protected $database;
          protected $user;
          protected $output;
          public function __construct($da tabase,$user,$o utput) {
          $this->database = $database;
          $this->user = $user;
          $this->output = $output;
          }
          }
          >
          And a child that looks like this:
          Class Status extends State {
          public function __construct($da tabase,$user,$o utput) {
          parent::__const ruct($database, $user,$output);
          }
          }
          >
          >
          This seems to work OK. But I'm getting confused when thinking about
          adding a new child who then has to contruct the parent again. Isn't
          this getting away from the whole purpose of inheritence? It seems like
          this is recreating the parent with every new child that come along.
          Would it be possible to just have a single instance of the parent that
          all children extended or am I missing the point here?
          >
          >
          Thanks
          >
          Jon
          >
          Jim,

          I think you're confusing programming inheritance with runtime
          inheritance. Two different child objects will have two different parent
          objects.

          For instance - let's say you have the parent class 'Person'. As
          children of that class you have 'Employee' and 'Consultant'. Child
          classes of 'Employee' are 'HourlyEmployee ' and 'SalariedEmploy ee'.

          They could have attributes such as:

          Person: Name, Address, etc.
          Employee: Employee ID, Department
          HourlyEmployee: Hourly rate
          SalariedEmploye e: Weekly salary
          Consultant: Consulting rate

          Now - you do:

          $tom = new HourlyEmployee( "Tom Jones", ...);
          $dick = new SalariedEmploye e("Dick Anderson", ...);
          $harry = new Consultant("Har ry Smith", ...);

          You would need three separate Person objects. You wouldn't want just
          one Person object - you wouldn't be able to keep all three names in it.

          Does this help?


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • reandeau

            #6
            Re: Child construct parent

            I think you're confusing programming inheritance with runtime
            inheritance. Two different child objects will have two different parent
            objects.
            >
            For instance - let's say you have the parent class 'Person'. As
            children of that class you have 'Employee' and 'Consultant'. Child
            classes of 'Employee' are 'HourlyEmployee ' and 'SalariedEmploy ee'.
            >
            They could have attributes such as:
            >
            Person: Name, Address, etc.
            Employee: Employee ID, Department
            HourlyEmployee: Hourly rate
            SalariedEmploye e: Weekly salary
            Consultant: Consulting rate
            >
            Now - you do:
            >
            $tom = new HourlyEmployee( "Tom Jones", ...);
            $dick = new SalariedEmploye e("Dick Anderson", ...);
            $harry = new Consultant("Har ry Smith", ...);
            >
            You would need three separate Person objects. You wouldn't want just
            one Person object - you wouldn't be able to keep all three names in it.
            >
            Does this help?
            >
            Yea that helps tremendously. I was getting caught up with my runtime
            program needing to look like my diagram. There was only one parent in
            my diagram, so I was thinking "why should there be multiple parent
            objects during runtime". I can see from what you stated above that
            there is a need to have a parent object for each child to keep the
            values stored in inherited variables associated with their prospecitve
            child.

            I think a big part of my issue here was that old procedural programmer
            coming out in me and wanting to make the code as efficient (read: as
            short) as possible. Creating all of these parent objects just didn't
            seem efficient and I had gotten caught up in the idea that inheritence
            must be purely for efficiency. But I can see now that, like most
            things OO, inheritence is there to help make your objects as resusable
            as possible- especially if done correctly.

            Thanks so much for your help.

            Jon

            Comment

            • Richard Levasseur

              #7
              Re: Child construct parent


              Dikkie Dik wrote:
              An exception always has to have a message, so it enforces one by
              requiring it in the constructor's parameters. This message is kept
              safely inside, so it is effectively read-only. Not even a subclass can
              touch it.
              This is incorrect:
              * The properties of the PHP5 Exception class are all protected. They
              can be directly accessed by derived classes, but not outside the class.
              * The constructor of a PHP5 Exception defaults to (null, 0), so niether
              argument is required.
              * It should be noted that the methods of the exception class cannot be
              overridden, they are declared 'final,' (except __toString)

              See http://us2.php.net/manual/en/language.exceptions.php

              Comment

              Working...