473,387 Members | 3,810 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,387 software developers and data experts.

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
Jul 17 '05 #1
4 2241
Phil Powell wrote:
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?


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

--
Justin Koivisto - sp**@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
Jul 17 '05 #2
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 ()

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


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

Jul 17 '05 #3
so*****@erols.com (Phil Powell) wrote in message news:<1c**************************@posting.google. com>...

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


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
Jul 17 '05 #4
Justin Koivisto <sp**@koivi.com> wrote in message news:<9%****************@news7.onvoy.net>...
Phil Powell wrote:
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?


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

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
Jul 17 '05 #5

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

Similar topics

2
by: Scott Townsend | last post by:
I'm trying to create a class library of a bunch of objects and I'm looking for a way to diagram and document the objects. Is there a tool that people use to do this? I've seen many Database...
6
by: woosu | last post by:
Hello ladies and gentlemen. I have a relatively simple problem that I've been unable to solve. In the interest of learning C++, I've decided to write a simple game. The basis for the game is...
1
by: Jim McNeely | last post by:
I'm trying some experiments with PostgreSQL 7.3.4 with inheritance. What I read is that if you implement triggers on a parent table, those same triggers won't fire if you add records to a child...
4
by: Timothy V | last post by:
Hi, I'm new to C# but i know a little of C++. My problem is inheritance within c#, im not sure how to do it. For example, i know that a class can only inherit one base class. But what if i want to...
7
by: Ryan Shaw | last post by:
I’m having a small problem with inheritance with a hierarchy of classes The example is Class Class Private m_classB as Class Class Class End Clas End Clas
2
by: Bishoy George | last post by:
In a web application using asp.net 2.0 All my classes are partial classes. - I noticed that partial class cannot be inherited... is that true? - I tried to remove the partial keyword , and I...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
4
by: mountain.dog | last post by:
Hi There, I'm trying to understand inheriting properties object and I'm not fully understanding the ways to accomplish this. For example, I have a PHP array that is generate in "class a". I...
3
by: Immortal Nephi | last post by:
I write three classes. The name of class are A, B, and C. Class A and Class B are inaccessible to the client. Class C is accessible to the client. Class B has inheritance to Class A. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.