extending classes and constructors...

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

    #1

    extending classes and constructors...

    Hi...

    Consider the following code:

    -----8<-----8<-----8<-----8<-----

    class A
    {
    var $str_text;

    function A()
    {
    echo "Class A, constructor A";
    $this->str_text = "I am in class A";
    echo "A: $this->str_text";
    }
    }

    class B extends A
    {
    var $str_more_text;

    function B()
    {
    echo "Class B, constructor B";
    $this->str_more_tex t = "I am in class B";
    var_dump($this->str_text);
    var_dump($this->str_more_text) ;
    }
    }

    $cls_new = new B;

    ----->8----->8----->8----->8-----

    The constructor for A is not called. Is it possible to call the constructor
    of the parent class? I have an extended class that is dependant on data
    mined in the parent class's constructor...

    Plankmeister.


  • Keith Bowes

    #2
    Re: extending classes and constructors...

    The Plankmeister wrote:[color=blue]
    > Hi...
    >
    > Consider the following code:
    >
    > -----8<-----8<-----8<-----8<-----
    >
    > class A
    > {
    > var $str_text;
    >
    > function A()
    > {
    > echo "Class A, constructor A";
    > $this->str_text = "I am in class A";
    > echo "A: $this->str_text";
    > }
    > }
    >
    > class B extends A
    > {
    > var $str_more_text;
    >
    > function B()
    > {
    > echo "Class B, constructor B";
    > $this->str_more_tex t = "I am in class B";
    > var_dump($this->str_text);
    > var_dump($this->str_more_text) ;
    > }
    > }
    >
    > $cls_new = new B;
    >
    > ----->8----->8----->8----->8-----
    >
    > The constructor for A is not called. Is it possible to call the constructor
    > of the parent class? I have an extended class that is dependant on data
    > mined in the parent class's constructor...
    >
    > Plankmeister.
    >
    >[/color]

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Comment

    • The Plankmeister

      #3
      Re: extending classes and constructors...


      "Keith Bowes" <do.not@spam.me > wrote in message
      news:x9aUb.3653 $cx3.1602@fe01. usenetserver.co m...[color=blue]
      > The Plankmeister wrote:[color=green]
      > > Hi...
      > >
      > > Consider the following code:
      > >
      > > -----8<-----8<-----8<-----8<-----
      > >
      > > class A
      > > {
      > > var $str_text;
      > >
      > > function A()
      > > {
      > > echo "Class A, constructor A";
      > > $this->str_text = "I am in class A";
      > > echo "A: $this->str_text";
      > > }
      > > }
      > >
      > > class B extends A
      > > {
      > > var $str_more_text;
      > >
      > > function B()
      > > {
      > > echo "Class B, constructor B";
      > > $this->str_more_tex t = "I am in class B";
      > > var_dump($this->str_text);
      > > var_dump($this->str_more_text) ;
      > > }
      > > }
      > >
      > > $cls_new = new B;
      > >
      > > ----->8----->8----->8----->8-----
      > >
      > > The constructor for A is not called. Is it possible to call the[/color][/color]
      constructor[color=blue][color=green]
      > > of the parent class? I have an extended class that is dependant on data
      > > mined in the parent class's constructor...
      > >
      > > Plankmeister.
      > >
      > >[/color]
      >
      > http://us3.php.net/manual/en/keyword.parent.php?
      >[/color]

      Absolutely spot on. Ta muchly : )


      Comment

      Working...