class can not inherit from another class, why ?

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

    #1

    class can not inherit from another class, why ?

    Is this a valid class definition?

    class McBase {

    // 06-14-04 - I may or may not later add something here. This will
    // be the top of the PDS content management system hierarchy. I'm
    // leaving it blank for now but want it here for future flexibility.

    }




    I use it later on this line:

    class McBasic extends McBase {


    but I get this error:

    Fatal error: Class mcbasic: Cannot inherit from undefined class mcbase
    in /usr/local/www/vhosts/publicpen.com/htdocs/designer/ppKernel/McBasic.php
    on line 5
  • Michael Fesser

    #2
    Re: class can not inherit from another class, why ?

    .oO(lawrence)
    [color=blue]
    >Is this a valid class definition?
    >
    >class McBase {
    >[...][/color]

    Yes.
    [color=blue]
    >I use it later on this line:
    >
    >class McBasic extends McBase {
    >
    >
    >but I get this error:
    >
    >Fatal error: Class mcbasic: Cannot inherit from undefined class mcbase
    >in /usr/local/www/vhosts/publicpen.com/htdocs/designer/ppKernel/McBasic.php
    >on line 5[/color]

    The question is: Are both classes stored in the same file? Apparently
    not. So you have to include the base-class before you can extend it,
    e.g.

    require_once 'mcbase.php';

    class McBasic extends McBase {...}

    HTH
    Micha

    Comment

    • lawrence

      #3
      Re: class can not inherit from another class, why ?

      Michael Fesser <netizen@gmx.ne t> wrote in message[color=blue][color=green]
      > >Fatal error: Class mcbasic: Cannot inherit from undefined class mcbase
      > >in /usr/local/www/vhosts/publicpen.com/htdocs/designer/ppKernel/McBasic.php
      > >on line 5[/color]
      >
      > The question is: Are both classes stored in the same file? Apparently
      > not. So you have to include the base-class before you can extend it,
      > e.g.
      >
      > require_once 'mcbase.php';[/color]



      Thanks. Now I got it working. But McBasic doesn't seem to have its
      constructor called when it is the parent of another class. I've been
      trying to test, as you'll see here:

      class McBasic extends McBase {


      var $controllerForA ll = null;
      var $resultsObject = null;
      var $notifyObject = null;
      var $historyObject = null;



      /**
      *
      *
      * 06-14-04 - most objects in the PDS content management system should
      be
      * descended from McBasic. This provides basic servies to all its
      child
      * classes. All the classes that themselves contain no other objects
      can
      * safely be put here, and then become services available to all child
      * classes.
      *
      *
      *
      *
      */
      function McBasic() {
      // a test for debugging
      echo "hello<br>" ;
      $this->controllerForA ll = & getController() ;

      if (is_object($thi s->controllerForA ll)) {
      echo "this is an object";
      } else {
      echo "this is not an object";
      }


      $this->resultsObjec t = &
      $this->controllerForA ll->getObject("McR esults", " in the constructor
      of McBasic.");
      $this->notifyObject = &
      $this->controllerForA ll->getObject("McN otify", " in the constructor of
      McBasic.");
      $this->historyObjec t = &
      $this->controllerForA ll->getObject("McH istory", " in the constructor
      of McBasic.");
      }
      }









      But in the class McArrangements, I have this line:

      class McArrangements extends McBasic {

      Yes McArrangements throws an error whenever I use the 4 objects that
      are set in the constructor of McBasic. The error says that there is no
      such object. For instance, I get an error on this line in
      McArrangements:

      $this->resultsObjec t->error("In setControlPanel CbId(), in the class
      McArrangements, we expected the method's parameter to be a number, but
      instead we got this: $cbId ", "McArrangements ");


      So I decided to write a test for McBasic, and the test revealed that
      everything was working for McBasic. In the test, I call McBasic
      directly, and at the end of the constructor I test to see if
      $this->resultsObjec t is an object. It tests true. Yet when I refer to
      $this->resultsObjec t in McArrangements, I get "No such object". How
      can this be?

      Comment

      • Berislav Lopac

        #4
        Re: class can not inherit from another class, why ?

        lawrence wrote:[color=blue]
        > Thanks. Now I got it working. But McBasic doesn't seem to have its
        > constructor called when it is the parent of another class. I've been
        > trying to test, as you'll see here:[/color]

        <snip>
        [color=blue]
        > So I decided to write a test for McBasic, and the test revealed that
        > everything was working for McBasic. In the test, I call McBasic
        > directly, and at the end of the constructor I test to see if
        > $this->resultsObjec t is an object. It tests true. Yet when I refer to
        > $this->resultsObjec t in McArrangements, I get "No such object". How
        > can this be?[/color]

        Are you using PHP5? Try using _construct().

        According to the Zend articles, the old way of defining constructors should
        work as well, but there might be some problems when calling parent's
        contructor.

        Berislav


        Comment

        • Berislav Lopac

          #5
          Re: class can not inherit from another class, why ?

          Berislav Lopac wrote:[color=blue]
          > lawrence wrote:[color=green]
          >> Thanks. Now I got it working. But McBasic doesn't seem to have its
          >> constructor called when it is the parent of another class. I've been
          >> trying to test, as you'll see here:[/color]
          >
          > <snip>
          >[color=green]
          >> So I decided to write a test for McBasic, and the test revealed that
          >> everything was working for McBasic. In the test, I call McBasic
          >> directly, and at the end of the constructor I test to see if
          >> $this->resultsObjec t is an object. It tests true. Yet when I refer to
          >> $this->resultsObjec t in McArrangements, I get "No such object". How
          >> can this be?[/color]
          >
          > Are you using PHP5? Try using _construct().[/color]

          Oops, that should be __construct() (two underscores).

          Berislav


          Comment

          • Phil Powell

            #6
            Re: class can not inherit from another class, why ?

            lkrubner@geocit ies.com (lawrence) wrote in message news:<da7e68e8. 0407271336.3076 66a9@posting.go ogle.com>...[color=blue]
            > Michael Fesser <netizen@gmx.ne t> wrote in message[color=green][color=darkred]
            > > >Fatal error: Class mcbasic: Cannot inherit from undefined class mcbase
            > > >in /usr/local/www/vhosts/publicpen.com/htdocs/designer/ppKernel/McBasic.php
            > > >on line 5[/color]
            > >
            > > The question is: Are both classes stored in the same file? Apparently
            > > not. So you have to include the base-class before you can extend it,
            > > e.g.
            > >
            > > require_once 'mcbase.php';[/color]
            >
            >
            >
            > Thanks. Now I got it working. But McBasic doesn't seem to have its
            > constructor called when it is the parent of another class. I've been
            > trying to test, as you'll see here:[/color]

            [snip]

            If you're running PHP 4+ and not PHP 5 use the parent keyword to run
            the parent's constructor in the child's constructor:

            [PHP]
            class Parent {

            function Parent() {
            // DO STUFF HERE
            }

            }

            class Child extends Parent {

            function Child() {
            parent::Parent( );
            }

            }
            [/PHP]

            Phil

            Comment

            • lawrence

              #7
              Re: class can not inherit from another class, why ?

              soazine@erols.c om (Phil Powell) wrote in message news:<1cdca2a7. 0407281516.a2b7 d2e@posting.goo gle.com>...[color=blue]
              > lkrubner@geocit ies.com (lawrence) wrote in message news:<da7e68e8. 0407271336.3076 66a9@posting.go ogle.com>...[color=green]
              > > Michael Fesser <netizen@gmx.ne t> wrote in message[color=darkred]
              > > > >Fatal error: Class mcbasic: Cannot inherit from undefined class mcbase
              > > > >in /usr/local/www/vhosts/publicpen.com/htdocs/designer/ppKernel/McBasic.php
              > > > >on line 5
              > > >
              > > > The question is: Are both classes stored in the same file? Apparently
              > > > not. So you have to include the base-class before you can extend it,
              > > > e.g.
              > > >
              > > > require_once 'mcbase.php';[/color]
              > >
              > >
              > >
              > > Thanks. Now I got it working. But McBasic doesn't seem to have its
              > > constructor called when it is the parent of another class. I've been
              > > trying to test, as you'll see here:[/color]
              >
              > [snip]
              >
              > If you're running PHP 4+ and not PHP 5 use the parent keyword to run
              > the parent's constructor in the child's constructor:
              >
              > [PHP]
              > class Parent {
              >
              > function Parent() {
              > // DO STUFF HERE
              > }
              >
              > }
              >
              > class Child extends Parent {
              >
              > function Child() {
              > parent::Parent( );
              > }
              >
              > }
              > [/PHP]
              >
              > Phil[/color]

              The parent keyword! I'd never heard of that! That is a terrific tip,
              thanks much.

              I got it work like this: McArrangements had a constructor that wasn't
              doing much, just setting a variable that could be set elsewhere. So I
              rewrote it and deleted the constructor. Suddenly the constructor of
              McBasic was called. So, I reasoned, if you want to have the
              constructor of a parent class called, you are not allowed to have a
              constructor in the child class. But now I see that, quite reasonably,
              there is a way to call parent constructors, and still have the child
              have a constructor. Very good!

              Comment

              Working...