Jump to content

OO question


earachefl

Recommended Posts

Just get started with OO in PHP. If I create an object while on one page and then call another page, can I still access that first object, or is it destroyed when the second page is called? For example, if I have on page1.php:

 

$obj = new $MyObject();

 

and then call page2.php, is it possible to still access $obj while on that page?

Link to comment
https://forums.phpfreaks.com/topic/229168-oo-question/
Share on other sites

You would need to use a $_SESSION variable -

 

Page where object is created -

<?php
// the class definition must exist before the session_start() so that the object can be recreated 

session_start();

$_SESSION['obj'] = new $MyObject();

 

Page that references the object -

<?php
// the class definition must exist before the session_start() so that the object can be recreated 

session_start();

echo $_SESSION['obj']->some_property;

echo $_SESSION['obj']->some_method();

 

Link to comment
https://forums.phpfreaks.com/topic/229168-oo-question/#findComment-1180934
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.