For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > Passing data between class instances









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing data between class instances
dogsbarking@hotmail.com

2006-01-24, 6:56 pm

Usinge PHP 4.1.2

I have a contoller script that builds page objects based on their
properties stored in the db.

for example:

I have a useradmin object to allow users to modify user records in the
db.

I have two instances of the object being put into the current page.

The first checks to see if there is posted data from the form and it
then inserts or updates the db.

The second instance displays the form with the associated user data.

What I want is for the second instance to use any error messages from
the first instance.

I can use a global variable to pass the error messages to the second
instance. Is there any other way to pass data between instances?

Ken Chau

2006-01-24, 9:55 pm

In my experience, it's okay to have an application level (global)
object that handles logging, error reporting, sessions, etc...

ZeldorBlat

2006-01-24, 9:55 pm


dogsbarking@hotmail.com wrote:
> Usinge PHP 4.1.2
>
> I have a contoller script that builds page objects based on their
> properties stored in the db.
>
> for example:
>
> I have a useradmin object to allow users to modify user records in the
> db.
>
> I have two instances of the object being put into the current page.
>
> The first checks to see if there is posted data from the form and it
> then inserts or updates the db.
>
> The second instance displays the form with the associated user data.
>
> What I want is for the second instance to use any error messages from
> the first instance.
>
> I can use a global variable to pass the error messages to the second
> instance. Is there any other way to pass data between instances?


Whether or not it's "correct" by a traditional OOP standard, PHP (well,
5+ anyway) allows objects to access the private and protected
properties and methods of another instance of the same class. You
would expect this to work with static methods, but not object methods.
But it works with both. So:

class foo {
private $x;

/* You would expect this to work correctly, since
there's no object context ($this) inside a static
method; it applies to all instances of foo.
*/
public static function setXStatic(foo $obj, $newX) {
$obj->x = $newX;
}

/* You'd think that this wouldn't work, because
from the perspective of $this, $obj is a totally
different instance and we shouldn't be able to
access a private property. But PHP allows
it anyway:
*/
public function setXInstance(foo $obj, $newX) {
$obj->x = $newX;
}
}

Ken Chau

2006-01-25, 6:57 pm

Quoting original post:

"Usinge PHP 4.1.2" ...

We're gonna have to come up with a better answer than a PHP5 solution.
So far, I have not seen anything that compares with the proposed global
object idea yet... can you store variables inside a class method
perhaps in PHP 4?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com