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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
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; } } |
|
|||
|
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? |