Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #2337 > unrolled thread

Preserving an object

Started bybruceaj <bruceaj@bellsouth.net>
First post2011-06-24 19:45 -0700
Last post2011-06-25 08:02 -0400
Articles 8 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Preserving an object bruceaj <bruceaj@bellsouth.net> - 2011-06-24 19:45 -0700
    Re: Preserving an object The Natural Philosopher <tnp@invalid.invalid> - 2011-06-25 09:27 +0100
      Re: Preserving an object Jivanmukta <jivanmukta@poczta.onet.pl> - 2011-06-25 04:06 -0700
        Re: Preserving an object Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-25 08:08 -0400
          Re: Preserving an object bruceaj <bruceaj@bellsouth.net> - 2011-06-25 06:37 -0700
            Re: Preserving an object Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-25 13:10 -0400
            Re: Preserving an object The Natural Philosopher <tnp@invalid.invalid> - 2011-06-26 04:18 +0100
    Re: Preserving an object Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-25 08:02 -0400

#2337 — Preserving an object

Frombruceaj <bruceaj@bellsouth.net>
Date2011-06-24 19:45 -0700
SubjectPreserving an object
Message-ID<fac5cb7c-2af6-4468-85ac-57067de9b2ca@q15g2000yqk.googlegroups.com>
I am creating an object in my "index.php". I want to use this object
in many of my php pages. How can I preserve this object so other pages
can use it and I don't have to recreate it??

Thanks...

Bruce

[toc] | [next] | [standalone]


#2338

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-06-25 09:27 +0100
Message-ID<iu464v$8sv$1@news.albasani.net>
In reply to#2337
bruceaj wrote:
> I am creating an object in my "index.php". I want to use this object
> in many of my php pages. How can I preserve this object so other pages
> can use it and I don't have to recreate it??
> 
> Thanks...
> 
> Bruce

Put it in a separate file and include it?

[toc] | [prev] | [next] | [standalone]


#2339

FromJivanmukta <jivanmukta@poczta.onet.pl>
Date2011-06-25 04:06 -0700
Message-ID<a3c0f9cc-3b28-4546-9972-0e19a8755b4c@g16g2000yqg.googlegroups.com>
In reply to#2338
> Put it in a separate file and include it?

IMHO it is not enough. Object should be stored in $_SESSION.
I have similar problem.
I tried to solve it this way:
File 1:
session_start();
...
$_SESSION['statement'] = $statement; // object

File 2:
session_start();
function __autoload($className) {
  require_once 'class.' . strtolower($className) . '.php';
}
$statement = $_SESSION['statement'];

but I receive such error:

object (__PHP_Incomplete_class) #1 (8) {
  ["__PHP_Incomplete_Class_Name"] => string(9) "Statement" etc.

Could anybody help me?

[toc] | [prev] | [next] | [standalone]


#2341

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-06-25 08:08 -0400
Message-ID<iu4j48$2h6$1@dont-email.me>
In reply to#2339
On 6/25/2011 7:06 AM, Jivanmukta wrote:
>> Put it in a separate file and include it?
>
> IMHO it is not enough. Object should be stored in $_SESSION.

Don't worry about TNP - it's a typical response from him.  He doesn't 
even know the difference between a class and an object.

> I have similar problem.
> I tried to solve it this way:
> File 1:
> session_start();
> ...
> $_SESSION['statement'] = $statement; // object
>
> File 2:
> session_start();
> function __autoload($className) {
>    require_once 'class.' . strtolower($className) . '.php';
> }
> $statement = $_SESSION['statement'];
>
> but I receive such error:
>
> object (__PHP_Incomplete_class) #1 (8) {
>    ["__PHP_Incomplete_Class_Name"] =>  string(9) "Statement" etc.
>
> Could anybody help me?

You have to have the class file loaded BEFORE starting the session so 
the interpreter knows how to deserialize the $_SESSION object.

P.S. I don't like autoload.  It causes too many problems, like 
performance from having to search multiple directories, or even loading 
the wrong file because someone stuck a file with the same name in 
another directory.  Even changing directory permissions can cause problems.

And, as you see, it doesn't work with sessions.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#2342

Frombruceaj <bruceaj@bellsouth.net>
Date2011-06-25 06:37 -0700
Message-ID<d4c69c3f-37b6-4c06-bcd5-f25e543e2548@16g2000yqy.googlegroups.com>
In reply to#2341
On Jun 25, 8:08 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 6/25/2011 7:06 AM, Jivanmukta wrote:
>
> >> Put it in a separate file and include it?
>
> > IMHO it is not enough. Object should be stored in $_SESSION.
>
> Don't worry about TNP - it's a typical response from him.  He doesn't
> even know the difference between a class and an object.
>
>
>
>
>
>
>
>
>
> > I have similar problem.
> > I tried to solve it this way:
> > File 1:
> > session_start();
> > ...
> > $_SESSION['statement'] = $statement; // object
>
> > File 2:
> > session_start();
> > function __autoload($className) {
> >    require_once 'class.' . strtolower($className) . '.php';
> > }
> > $statement = $_SESSION['statement'];
>
> > but I receive such error:
>
> > object (__PHP_Incomplete_class) #1 (8) {
> >    ["__PHP_Incomplete_Class_Name"] =>  string(9) "Statement" etc.
>
> > Could anybody help me?
>
> You have to have the class file loaded BEFORE starting the session so
> the interpreter knows how to deserialize the $_SESSION object.
>
> P.S. I don't like autoload.  It causes too many problems, like
> performance from having to search multiple directories, or even loading
> the wrong file because someone stuck a file with the same name in
> another directory.  Even changing directory permissions can cause problems.
>
> And, as you see, it doesn't work with sessions.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Well, the object I'm trying to preserve is a database connection. SO,
from reading the above responses, it SEEMS that $_SESSIONs will NOT
work. Bottom line, I can't do it.

Thanks for the responses....

Bruce

[toc] | [prev] | [next] | [standalone]


#2343

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-06-25 13:10 -0400
Message-ID<iu54pt$jbb$1@dont-email.me>
In reply to#2342
On 6/25/2011 9:37 AM, bruceaj wrote:
> On Jun 25, 8:08 am, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 6/25/2011 7:06 AM, Jivanmukta wrote:
>>
>>>> Put it in a separate file and include it?
>>
>>> IMHO it is not enough. Object should be stored in $_SESSION.
>>
>> Don't worry about TNP - it's a typical response from him.  He doesn't
>> even know the difference between a class and an object.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> I have similar problem.
>>> I tried to solve it this way:
>>> File 1:
>>> session_start();
>>> ...
>>> $_SESSION['statement'] = $statement; // object
>>
>>> File 2:
>>> session_start();
>>> function __autoload($className) {
>>>     require_once 'class.' . strtolower($className) . '.php';
>>> }
>>> $statement = $_SESSION['statement'];
>>
>>> but I receive such error:
>>
>>> object (__PHP_Incomplete_class) #1 (8) {
>>>     ["__PHP_Incomplete_Class_Name"] =>    string(9) "Statement" etc.
>>
>>> Could anybody help me?
>>
>> You have to have the class file loaded BEFORE starting the session so
>> the interpreter knows how to deserialize the $_SESSION object.
>>
>> P.S. I don't like autoload.  It causes too many problems, like
>> performance from having to search multiple directories, or even loading
>> the wrong file because someone stuck a file with the same name in
>> another directory.  Even changing directory permissions can cause problems.
>>
>> And, as you see, it doesn't work with sessions.
>>
>
> Well, the object I'm trying to preserve is a database connection. SO,
> from reading the above responses, it SEEMS that $_SESSIONs will NOT
> work. Bottom line, I can't do it.
>
> Thanks for the responses....
>
> Bruce

No.  Database connections are closed when the script terminates.

Transactional programming, like for web page scripts, is much different 
than non-transactional programming.  Basically you can pass data from 
one script to another, but you cannot pass any external resources.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#2344

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-06-26 04:18 +0100
Message-ID<iu68dc$lg6$6@news.albasani.net>
In reply to#2342
bruceaj wrote:
> On Jun 25, 8:08 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 6/25/2011 7:06 AM, Jivanmukta wrote:
>>
>>>> Put it in a separate file and include it?
>>> IMHO it is not enough. Object should be stored in $_SESSION.
>> Don't worry about TNP - it's a typical response from him.  He doesn't
>> even know the difference between a class and an object.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> I have similar problem.
>>> I tried to solve it this way:
>>> File 1:
>>> session_start();
>>> ...
>>> $_SESSION['statement'] = $statement; // object
>>> File 2:
>>> session_start();
>>> function __autoload($className) {
>>>    require_once 'class.' . strtolower($className) . '.php';
>>> }
>>> $statement = $_SESSION['statement'];
>>> but I receive such error:
>>> object (__PHP_Incomplete_class) #1 (8) {
>>>    ["__PHP_Incomplete_Class_Name"] =>  string(9) "Statement" etc.
>>> Could anybody help me?
>> You have to have the class file loaded BEFORE starting the session so
>> the interpreter knows how to deserialize the $_SESSION object.
>>
>> P.S. I don't like autoload.  It causes too many problems, like
>> performance from having to search multiple directories, or even loading
>> the wrong file because someone stuck a file with the same name in
>> another directory.  Even changing directory permissions can cause problems.
>>
>> And, as you see, it doesn't work with sessions.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
> 
> Well, the object I'm trying to preserve is a database connection. SO,
> from reading the above responses, it SEEMS that $_SESSIONs will NOT
> work. Bottom line, I can't do it.
> 
> Thanks for the responses....
> 
> Bruce
Now if you had said that...to start with

No, a database connection will be broken when the script terminates.

You might carry a pointer to it, but it wont be there to point to :-)

Either reopen the connection, or build a layer in another language that 
will keep connections open when PHP terminates.

But is very little overhead reopening a database connection.



[toc] | [prev] | [next] | [standalone]


#2340

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-06-25 08:02 -0400
Message-ID<iu4iok$vsf$1@dont-email.me>
In reply to#2337
On 6/24/2011 10:45 PM, bruceaj wrote:
> I am creating an object in my "index.php". I want to use this object
> in many of my php pages. How can I preserve this object so other pages
> can use it and I don't have to recreate it??
>
> Thanks...
>
> Bruce

It's difficult because PHP does not handle serialization of objects very 
well.

The correct way would be to store it in the $_SESSION.  However, you 
have to be careful that the object doesn't save any external resources 
such as database links, which will not be valid when it is reloaded.

Additionally, when your page terminates, the PHP calls the object's 
destructor, but does not call the object's constructor when it is read 
back into storage.  This means the constructor will be called once for 
the object, but the destructor called many times.

One other thing - the $_SESSION isn't real efficient for larger data 
structures; I wouldn't recommend storing large objects in it.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web