473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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::PagOptionsView($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::AssocView($id, $errorArray);[/PHP]

PagOptionsView's constructor instantiates a DBActionPerformer object as
so:
[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
so:
[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:

...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

Jul 17 '05 #1
3 1455
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

Jul 17 '05 #2
I GOT IT!!

using class DBActionPerformer:

[PHP]
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();
}

// 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

Jul 17 '05 #3
I would strongly advice you to take a look at how Plog is doing with
MVC, they are fantastic!
www.plogworld.net
Go to download a copy, and you can use there structure to build
anything!

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
2
by: Darryn Ross | last post by:
Hi.. I have a standard windows app in C#. The project contains a two forms and one class that i have created. I want both of the forms to inherit from the class but i am getting an error reading...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
5
by: Mac via DotNetMonster.com | last post by:
Hi all, I have a creating a my own tabpage class (MyTabPage) which inherits the .Net TabPage class. In the relevant event I want to loop through the collection of TabPages and then when I...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
1
by: bill | last post by:
What's up with inheritance in code behind? Is it reliable, or should I skip it and forget about using inheritance in web forms until they get it right? I'm using VB.NET 2005 I have a web form...
13
by: interec | last post by:
I have some code in Java that I need to translate into C++. My Java code defines a class hierarchy as follows: // interface IA public interface IA { public abstract void doA(); } //...
2
by: Wilson | last post by:
Hi, a very simple question. I am trying to understand inheritance using c++ and dont cee how i could use three classes to create an accounting program using inheritance. e.g one class containing...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.