PHP, classes, "extends", inheritance.. help!!

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

    PHP, classes, "extends", inheritance.. help!!

    [PHP]
    class Parent {
    var $errorArray = array();

    function Parent() {
    return true;
    }

    function getErrorArray() {
    return $this->errorArray;
    }

    function setErrorArray($ errorArray) {
    $this->errorArray = $this->errorArray + $errorArray;
    }

    }

    class Child1 extends Parent {

    function Child1() {
    return true;
    }

    function doStuff() {
    if ($this->isWrong) {
    $this->setErrorArray( array('action' => 'Oops!'));
    }
    }

    }

    class Child2 extends Parent {

    function Child2() {
    return true;
    }

    function doStuff() {
    if ($this->isWrong) {
    $this->setErrorArray( array('action' => 'Oops!'));
    }
    }

    }

    $child1 =& new Child();
    $child1->doStuff();
    print_r($child1->getErrorArray( ));
    $child1 = null;

    $child2 =& new Child();
    $child2->doStuff();
    print_r($child2->getErrorArray( ));
    $child2 = null;

    [/PHP]

    Ok, here is the simplest example of what I am trying to do. I have a
    near-pure-OO required setup of PHP where I am performing a Child1
    object instance method doStuff(). If something goes wrong it sets its
    parents $this->errorArray. I will then perform a Child2 object
    instance method doStuff(). Same rules apply.

    I want to see both errors filled out when I do my print_r() commands,
    but all I get is this:

    Array ()
    What am I doing wrong in my structure? I want all of my children
    classes of Parent all set up the same instance of $this->errorArray.
    Does it need to be static? What do I do?

    Phil
  • Justin Koivisto

    #2
    Re: PHP, classes, "extends&q uot;, inheritance.. help!!

    Phil Powell wrote:
    [color=blue]
    > function setErrorArray($ errorArray) {
    > $this->errorArray = $this->errorArray + $errorArray;
    > }
    >
    > I want to see both errors filled out when I do my print_r() commands,
    > but all I get is this:
    >
    >
    Array ()
    >
    > What am I doing wrong in my structure? I want all of my children
    > classes of Parent all set up the same instance of $this->errorArray.
    > Does it need to be static? What do I do?[/color]

    I think what you want is:
    function setErrorArray($ errorArray) {
    $this->errorArray = array_merge($th is->errorArray,$er rorArray);
    }

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.
    Official Google SERPs SEO Competition: http://www.koivi.com/serps.php

    Comment

    • Alex Farran

      #3
      Re: PHP, classes, "extends&q uot;, inheritance.. help!!

      [color=blue]
      > Ok, here is the simplest example of what I am trying to do. I have a
      > near-pure-OO required setup of PHP where I am performing a Child1
      > object instance method doStuff(). If something goes wrong it sets its
      > parents $this->errorArray. I will then perform a Child2 object
      > instance method doStuff(). Same rules apply.[/color]
      [color=blue]
      > I want to see both errors filled out when I do my print_r() commands,
      > but all I get is this:[/color]
      [color=blue]
      >
      Array ()
      [/color]

      In your example code isWrong is never declared, so that's what you'd
      expect. If it was declared then I'd expect errors raised by an
      instance of a child class to be visible in that instance's errorArray,
      but not in the errorArray of a different instance of the same class.
      I gather that's not what you want.
      [color=blue]
      > What am I doing wrong in my structure? I want all of my children
      > classes of Parent all set up the same instance of $this->errorArray.
      > Does it need to be static? What do I do?[/color]

      You are confusing your object heirarchy, which concerns itself with
      relationships between distinct instances of classes, with your class
      heirarchy, which defines the structural relationships between
      different types.

      The parent - child relationship you are trying to implement using the
      class heirarchy, should instead be implemented as a relationship
      between distinct objects. Your parent class can stay as it is, but
      instead of extending that class, create a relationship between the
      parent object and each child object you instantiate.

      class Child {
      var $parent;

      function Child( &$parent ) {
      $this->parent =& $parent;
      }

      function doStuff() {
      if ($this->isWrong) {
      $this->parent->setErrorArray( array('action' => 'Oops!'));
      }
      }

      To read the errorArray you call the parent object's getErrorArrray( )
      method.

      --

      __o Alex Farran
      _`\<,_ Analyst / Programmer
      (_)/ (_) www.alexfarran.com

      Comment

      • Rahul Anand

        #4
        Re: PHP, classes, &quot;extends&q uot;, inheritance.. help!!

        soazine@erols.c om (Phil Powell) wrote in message news:<1cdca2a7. 0402111147.2f0f 2618@posting.go ogle.com>...[color=blue]
        >
        > Ok, here is the simplest example of what I am trying to do. I have a
        > near-pure-OO required setup of PHP where I am performing a Child1
        > object instance method doStuff(). If something goes wrong it sets its
        > parents $this->errorArray. I will then perform a Child2 object
        > instance method doStuff(). Same rules apply.
        >
        > I want to see both errors filled out when I do my print_r() commands,
        > but all I get is this:
        >
        >
        Array ()
        >
        > What am I doing wrong in my structure? I want all of my children
        > classes of Parent all set up the same instance of $this->errorArray.
        > Does it need to be static? What do I do?
        >
        > Phil[/color]

        Here you are creating two different objects.

        And different objects can not share information as such.
        You are extending both child class from same parent but this does not
        mean that they will share same object as parent.

        The two child class will have all different instance so can not share
        errors by populating an array of parent class.

        To share information among different objects you need to declare your
        variable and function as static.

        Or as another option you can instantiate a different error class and
        share this object through reference passing to all other function.

        Choice depends upon your requirement.

        Few comments:
        1. Its better to initialize class variables in Class constructor.
        As you can use some function call also in constructors.
        2. Delare all your variables with var keyword which you are going
        to use
        in your class.


        Hope it will help.

        --
        Cheers,
        Rahul Anand

        Comment

        • Phil Powell

          #5
          Re: PHP, classes, &quot;extends&q uot;, inheritance.. help!!

          Justin Koivisto <spam@koivi.com > wrote in message news:<9%vWb.266 $sS3.5995@news7 .onvoy.net>...[color=blue]
          > Phil Powell wrote:
          >[color=green]
          > > function setErrorArray($ errorArray) {
          > > $this->errorArray = $this->errorArray + $errorArray;
          > > }
          > >
          > > I want to see both errors filled out when I do my print_r() commands,
          > > but all I get is this:
          > >
          > >
          Array ()
          > >
          > > What am I doing wrong in my structure? I want all of my children
          > > classes of Parent all set up the same instance of $this->errorArray.
          > > Does it need to be static? What do I do?[/color]
          >
          > I think what you want is:
          > function setErrorArray($ errorArray) {
          > $this->errorArray = array_merge($th is->errorArray,$er rorArray);
          > }[/color]


          No that doesn't work unless $errorArray is itself an array for that to
          occur. turns out it wasn't setErrorArray that was the problem, my
          entire OO design was the problem.

          Phil

          Comment

          Working...