Re: OOP design question
| From: | Geoffrey Thompson | Date: | Mon, 05 Jan 2004 18:16:34 +0000 |
| Subject: | Re: OOP design question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-173771@lists.php.net to get a copy of this message | ||
"Mike Smith" <msmith@larrymethvin.com> wrote in message
news:20040105160140.76626.qmail@pb1.pair.com...
> I am starting to get a handle (I think) on OOP. I was wondering if
> anyone would care to comment on what I think I understand:
>
> For simplicity's sake class.php contains AddItem() and DisplayItems().
> Since submitting form data
>
> <?php
> include('class.php');
>
> If(!$_POST['submit']){ //Do I need to instantiate(?) $po everytime the
> page reloads?
> $po = new PO;
> }
If you instantiate on every page load, you will only have one item - the
last one added. So, you will need to instantiate only once, and then store
the object in a session variable - but beware! Assigning the local variable
to the object will make a copy of the object stored in the session, so you
will have to either
1). Set the session variable back to $po at the end of the page, as such:
$_SESSION[po] = $po;
or
2). Set the local variable via reference to the session variable when
initializing $po at the beginning of the page (which references the existing
object, and does NOT make a copy), as such:
$po = &$_SESSION[po]; // notice the & which sets $po by
reference, rather than making a copy.
>
>
> If($_POST['submit']){
> $po->AddItem();
> }
>
> <form action="$PHP_SELF" method="POST" >
> ...form fields and a submit button...
> </form>
>
> $po->DisplayItems();
>
> ?>
>
> Is this a good basic methodology (aside from checking the form fields
> for valid characters).
>
> Thanks,
> Mike Smith