Passing data between class instances

This is a discussion on Passing data between class instances within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Usinge PHP 4.1.2 I have a contoller script that builds page objects based on their properties stored in ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-25-2006
dogsbarking@hotmail.com
 
Posts: n/a
Default Passing data between class instances

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?

Reply With Quote
  #2 (permalink)  
Old 01-25-2006
Ken Chau
 
Posts: n/a
Default Re: Passing data between class instances

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

Reply With Quote
  #3 (permalink)  
Old 01-25-2006
ZeldorBlat
 
Posts: n/a
Default Re: Passing data between class instances


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;
}
}

Reply With Quote
  #4 (permalink)  
Old 01-25-2006
Ken Chau
 
Posts: n/a
Default Re: Passing data between class instances

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?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:55 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0