Preserving an object [message #174661] |
Sat, 25 June 2011 02:45 |
bruceaj
Messages: 30 Registered: September 2010
Karma: 0
|
Member |
|
|
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
|
|
|
Re: Preserving an object [message #174662 is a reply to message #174661] |
Sat, 25 June 2011 08:27 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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?
|
|
|
Re: Preserving an object [message #174663 is a reply to message #174662] |
Sat, 25 June 2011 11:06 |
Jivanmukta
Messages: 20 Registered: January 2011
Karma: 0
|
Junior Member |
|
|
> 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?
|
|
|
Re: Preserving an object [message #174664 is a reply to message #174661] |
Sat, 25 June 2011 12:02 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Preserving an object [message #174665 is a reply to message #174663] |
Sat, 25 June 2011 12:08 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Preserving an object [message #174666 is a reply to message #174665] |
Sat, 25 June 2011 13:37 |
bruceaj
Messages: 30 Registered: September 2010
Karma: 0
|
Member |
|
|
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
|
|
|
Re: Preserving an object [message #174667 is a reply to message #174666] |
Sat, 25 June 2011 17:10 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Preserving an object [message #174668 is a reply to message #174666] |
Sun, 26 June 2011 03:18 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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.
|
|
|