Problems with classes, inheritance, and PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    #1

    Problems with classes, inheritance, and PHP

    I'm having a serious problem in my application I simply cannot seem to
    fix, and it has to do with multiple inheritances whereby something is
    lost.

    Class EditView is a child of PagOptionsView
    Class Addview is a child of AssocView

    EditView's constructor will call a "super()" onto PagOptionsView as so:
    [PHP]parent::PagOpti onsView($id, $errorArray);[/PHP]
    EditView's constructor will next instantiate an AddView object for
    itself:
    [PHP]$this->addView =& new AddView($this->id, $this->errorArray);[/PHP]

    AddView's constructor will call a "super()" onto AssocView as so:
    [PHP]parent::AssocVi ew($id, $errorArray);[/PHP]

    PagOptionsView' s constructor instantiates a DBActionPerform er object as
    so:
    [PHP]$this->dbAP = new DBActionPerform er($id);[/PHP]
    AssocView's constructor instantiates a DBActionPerform er object as so:
    [PHP]$this->dbAP =& new DBActionPerform er($id);[/PHP]

    DBActionPerform er's constructor instantiates a DBConnection object as
    so:
    [PHP]$this->dbConnObj = new DBConnection($d bHost, $dbPort, $dbUser,
    $dbPwd, $dbDefaultName) ;[/PHP]

    Now, when I instantiate an EditView object, I have a method,
    displayHTML() that will display HTML of stuff. I can do this and
    everything works beautifully:

    [PHP]$result = $this->dbAP->select(); // RESULTSET EVERY TIME [/PHP]

    However, were I do this, again, in EditView's displayHTML() method:

    [PHP]$html .= $this->addView->displayHTML( );

    I get the following error:

    ...function on a non-object in ...
    This occurs because the following doesn't exist as an object:

    $this->addView->dbAP->dbConnObj
    Whereas this exists:

    $this->dbAP->dbConnObj
    What is going on? The DBConnection object is somehow nonexistent in
    one method of inheritance but found in another. I need someone who
    really REALLY knows their OO PHP (or can fake it good enough) to help
    me with this one, I'm totally stuck!

    Thanx
    Phil

  • Allan

    #2
    Re: Problems with classes, inheritance, and PHP

    Hi there,
    I've been using MVC model for lots of my works, looks like you are
    trying to do the same thing, I would sugguest you to split your
    database modual out your main modual, this can help your programme in a
    better structure.

    OK, step into your problem, I do always have your errors "function on a
    non-object in ...", and every time I got this error back it's because
    the query to the database failed. So do not think your databast object
    doesn't exist, it's because your database didn't get any thing back,
    use print_r to check everything you get back from database from the
    very low level.

    Secondly, as your description, you can get feedback from
    "$this->dbAP->select();", in this way it should certainly exist
    "$this->dbAP->dbConnObj" but not the "$this->addView->dbAP->dbConnObj".
    Try to review your code more carefully and if you think the code is
    correct, post some of your code here, and we can help you to find out
    where the problem is.

    Good luck!

    Allan

    Comment

    • comp.lang.php

      #3
      Re: Problems with classes, inheritance, and PHP

      I GOT IT!!

      using class DBActionPerform er:

      [PHP]
      class DBActionPerform er extends MethodGenerator ForActionPerfor mer {

      function DBActionPerform er($id, $errorArray) {
      global $dbHost, $dbPort, $dbUser, $dbPwd, $dbDefaultName;
      $this->id = $id;
      if (is_array($erro rArray)) $this->errorArray = $errorArray;
      $this->dbConnObj =& new DBConnection( $dbHost, $dbPort, $dbUser,
      $dbPwd, $dbDefaultName) ;
      }

      function &connect() {
      $this->dbConn = $this->dbConnObj->connect();
      }

      // HERE WAS THE CULPRIT!!
      /**
      * Disconnect from database
      *
      * @access public
      */
      function &disconnect( ) { // STATIC VOID METHOD
      if ($this->dbConn) $this->dbConnObj->close();
      // THIS CAUSED THE FATAL ERROR: $this->dbConnObj =
      null;
      $this->dbConn = null;
      }

      }

      [/PHP]

      Apparently what was happening the whole time was that I needed a
      persistent DB connection, however, I would have methods whereby
      disconnections would take place and instead of freeing the resource but
      keeping the object alive, the resources would be freed AND the object
      destroyed. Thus, reconnection, whenever warranted, would be
      impossible, simply because I was performing the connect() method on
      a... non-object!

      I also decided to allow for an option to keep persistent DB connections
      (not allowing even for a disconnect() method to be called) in certain
      methods so that the connection stays open as long as it needs to stay
      open until you're done with the DB.

      Phil

      Comment

      • Allan

        #4
        Re: Problems with classes, inheritance, and PHP

        I would strongly advice you to take a look at how Plog is doing with
        MVC, they are fantastic!

        Go to download a copy, and you can use there structure to build
        anything!

        Comment

        Working...