Problems with classes, inheritance, and PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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

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::PagOptionsView($id, $errorArray);[/PHP]
EditView's constructor will next instantiate an AddView object for
[PHP]$this->addView =& new AddView($this->id, $this->errorArray);[/PHP]

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

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

DBActionPerformer's constructor instantiates a DBConnection object as
[PHP]$this->dbConnObj = new DBConnection($dbHost, $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:

[Quote]...function on a non-object in ...[/Quote]

This occurs because the following doesn't exist as an object:


Whereas this exists:


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!


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!


Re: Problems with classes, inheritance, and PHP


using class DBActionPerformer:

class DBActionPerformer extends MethodGeneratorForActionPerformer {

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

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

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



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.


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

Site Timeline