Re: Persistent Objects

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

 



Thanks Nathan,

I guess that your answer raises a couple of questions.

1. Does the answer below mean no global persistant objects?
(Application scope) I guess that it does.

2. Is this an un-PHP way of doing things?

3. Is what I'm trying to do even worthwhile or should I just create
each form and its validation rules by hand?  (My gut feeling is that
its still worth doing as it centralizes form processing and reduces
redundancy on a code level)

4. Which would be more expensive recreating the object each time? Or
fetching it from the session each time? (I know that this probably
doesn't have a definative answer)

Anyway my feeling is it probably shouldn't go in the session as its
not a client object.

Comments are welcome.

Regards,

Wesley
On 7/15/07, Nathan Nobbe <quickshiftin@xxxxxxxxx> wrote:
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