Hi!
I have been playing with PHP for a while, although my current project is my first deep OOP program in PHP. I've done OOP elsewhere. So there is my problem, in pseudo-code, as briefly as possible:
I have a class in a script called ObjectOne.php:
Class ObjectOne
public static var allInstances;
__constructor() {
//do some stuff here.
if self::allInstances does not exist,
then self::allInstances = new array().
add this instance to the allInstances array.
}
Then, in another script, I have
include_once('ObjectOne.php');
$newOb1 = new ObjectOne();
$newOb2 = new ObjectOne();
$newOb3 = new ObjectOne();
which creates three instances of ObjectOne.
Afterward that's done, in a third script I have
include_once('ObjectOne.php');
print_r(count of ObjectOne::allInstances)
Should come back as 3, but instead comes back blank. Basically, I suspect that including 'ObjectOne.php' is redefining the class ObjectOne in my last script, and in so doing, erasing the static allInstances array. So the real heart of the matter is this: How do I import a class definition without re-executing the code to preserve static data members?
Any help is appreciated!
PHP: importing classes without redefining them
Moderator: General Moderators
Re: PHP: importing classes without redefining them
PHP is stateless, so setting a variable on one page will not cause it to be set when page 2 is requested.
Unless you store them in the special "address" $_SESSION.
www.php.net/session
Entire objects can be stored into the session like you want, just into serialize() & unserialize() some of the better frameworks like Zend offer more sophisticated features like automatic serializing of objects, etc.
Unless you store them in the special "address" $_SESSION.
www.php.net/session
Entire objects can be stored into the session like you want, just into serialize() & unserialize() some of the better frameworks like Zend offer more sophisticated features like automatic serializing of objects, etc.
Re: PHP: importing classes without redefining them
I was afraid of that- looks like I'm going to have to rethink my OOP strategy for my project. Thanks for the speedy reply!