Re: Persistent Objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Wesley,

I too have come from a Java [ and c++ ] background to PHP.  fear not; you
can create persistent objects PHP.
although there is no JVM to handle this automatically
object can be persisted by storing their references.  the
most natural place to store them is in the session.  and if i understand the
advice i was given a few weeks ago
this is *the PHP way* to do it since PHP was written as an implementation of
the *shared nothing paradigm *(did i say that correctly?).
Also, note that there are many ways the session can be stored.  PHP stores
the session on disk naively, but
this behavior can be overridden.  Sessions often times are stored in a
database instead.  And my thinking, though
im sure someone would love to correct me, is that sessions can be stored in
memory via a technology like
memcached.

-nathan

On 7/15/07, Wesley Acheson <wesley.acheson@xxxxxxxxx> wrote:

Hi,

At work we use Java so one thing is annoying me. Is there really no
way to create a persistent object in PHP?  As far as my understanding
goes each object will be recreated on each and every request.

The reason I was asking is I wanted to create a form object that would
be used as follows.

which would require recreating the from stuff three times at least
Once for a JS file. Once for a HTML output.  And once for Server side
validation)

Please note that the following is just an example off the top of my
head the code to do this hasn't been written yet and the exact
implemantation may be different.  It would be much better to only
create the object once.

<?php
...
Other stuff goes here.
...
//Create a new form
formReg = new Form ("registration");

regStyle = new RenderStyle(HTML_AND_JAVASCRIPT_TABLELESS);
formReg->setRenderStyle();

/**
* Set the username field and its Validation
*/
fieldUsername = new Field("username");
fieldUsername->setLabel("User Name");
fieldUsername->setType(FIELD_TEXT);
fieldUsername->setCompulsary(true);
fieldUsername->setUnique(true);
fieldUsername->setMinLength(6);
fieldUsername->setMaxLength(20);
fieldUsername->setMaxLengthError(fieldUsername->getMinLengthError());
fieldUsername->setValidCharachters(CHARACHTERS_A-Z_1-9);

formReg->addField(fieldUsername)
.....
//OTHER FIELD STUFF HERE
.....

//Adds the link to a generated JS file at the top;
if (frmReg->needsJSCode == true) {
  output->add(formReg->getJSValidationURL)
}
....
//CONTINUE WITH THE REST OF THE HTML PAGE
....

//Insert the form into the HTML
output->add(fromReg->getForm(regForm));
?>

In the custom JS file we need;
<?php
frmReg->getValidationAsJavascript()
?>

Then after this in the page that receives the form submission we again
need to get the validation rules which means recreating the the form
object at least three times.  This time to validate serverside.

<?php
session_start() {
....
if (!frmReg->validates)  {
  $_SESSION[VALIDATION_ERROR] = frmReg->getValidationError();
  $_SESSION[ERROR_FIELD] = frmReg->getValidationField();
...
//logging goes here
...
  HttpResponse::redirect ( REGISTRATION_URL) ;
....
} else {
...
  // Attempt to save to the DB
  // Redirect on error

  // log a successfull registration
...
  require_once (REGISTRAION_SUCCESS_PAGE);
}

?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux