Building on classes -- inheritance help needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mountain.dog@gmail.com

    Building on classes -- inheritance help needed

    Hi There,
    I'm trying to understand inheriting properties object and I'm not
    fully understanding the ways to accomplish this.

    For example, I have a PHP array that is generate in "class a". I
    would like to use this array with its values in "class b". When I
    create a new instance of "class b" I get the array but none of the
    values are present. Make sense? An explanation on how to accomplish
    this would be great. A simplified example is below. Any help /
    guidance would be greatly welcome. -- CH

    class a {
    public $one;

    public function someMethod() {
    $one = array("1");
    }
    }

    class b extends class a {
    public $one;
    public $two

    function someOtherMethod () {
    print_r($this->one);
    }
    }

    $display = new a;
    $displayb = new b($a);
    $displayb->someOtherMetho d();

    //outputs
    Array ( [0] =)

  • Toby A Inkster

    #2
    Re: Building on classes -- inheritance help needed

    mountain.dog wrote:
    $display = new a;
    $displayb = new b($a);
    $displayb->someOtherMetho d();
    You're misunderstandin g what inheritance is; so the second line of the
    above excerpt doesn't do what you think it does. Replace those three
    lines with:

    $displayb = new b();
    $displayb->someMethod() ;
    $displayb->someOtherMetho d();

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • Jerry Stuckle

      #3
      Re: Building on classes -- inheritance help needed

      mountain.dog@gm ail.com wrote:
      Hi There,
      I'm trying to understand inheriting properties object and I'm not
      fully understanding the ways to accomplish this.
      >
      For example, I have a PHP array that is generate in "class a". I
      would like to use this array with its values in "class b". When I
      create a new instance of "class b" I get the array but none of the
      values are present. Make sense? An explanation on how to accomplish
      this would be great. A simplified example is below. Any help /
      guidance would be greatly welcome. -- CH
      >
      class a {
      public $one;
      >
      public function someMethod() {
      $one = array("1");
      }
      }
      >
      class b extends class a {
      public $one;
      public $two
      >
      function someOtherMethod () {
      print_r($this->one);
      }
      }
      >
      $display = new a;
      $displayb = new b($a);
      $displayb->someOtherMetho d();
      >
      //outputs
      Array ( [0] =)
      >
      Two problems.

      First of all, $one in class 'b' overrides $one in class 'a'. So you end
      up with two variables named $one - one in 'a' and one in 'b'.

      Secondly, you don't need

      $display = new a;

      When you create an instance if 'b' you automatically get an instance of
      'a'. 'a' is part of 'b's implementation.


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

      Comment

      • mountain.dog@gmail.com

        #4
        Re: Building on classes -- inheritance help needed

        Thank you Toby. Your brief example helped clarify a lot. I appreciate
        your help. One question - what's the difference between these two
        instances?
        $displayb = new b();
        versus
        $displayb = new b;
        thanks!

        On Mar 21, 11:27 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
        wrote:
        mountain.dog wrote:
        $display = new a;
        $displayb = new b($a);
        $displayb->someOtherMetho d();
        >
        You're misunderstandin g what inheritance is; so the second line of the
        above excerpt doesn't do what you think it does. Replace those three
        lines with:
        >
        $displayb = new b();
        $displayb->someMethod() ;
        $displayb->someOtherMetho d();
        >
        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~http://tobyinkster.co.uk/contact
        Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
        >
        * = I'm getting there!

        Comment

        • Toby A Inkster

          #5
          Re: Building on classes -- inheritance help needed

          mountain.dog@gm ail.com wrote:
          One question - what's the difference between these two
          instances?
          $displayb = new b();
          versus
          $displayb = new b;
          In this case, no difference.

          You can however, write a class with a constructor function which takes one
          or more arguments, in which case the brackets are required:

          $displayb = new b('something', 123, 456);

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact
          Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

          * = I'm getting there!

          Comment

          Working...