Page 1 of 1
Stuck in limbo
Posted: Wed Mar 26, 2014 12:16 pm
by Revolution
confused...
image 4 files
a.php
b.php
c.php.
testclass.php
a/b/c.php all include testclass.php (i've test require/require_once/include/include_once)
testclass.php checks to see if the class exists
Code: Select all
if ( class_exists('testclass')) {
if the class does not exist... it creates it
class testclass {
public function __construct($teststring) {
$this->teststring = $teststring;
}
public function get_Search_Race() {
return $this->se_Search_Race;
}
public function teststring($teststring) {
$this->teststring = $teststring;
}
$testclass = new testclass("0");
the problem I have is... the class_exists seems to fail
the class is being recreated every time... in a/b/c.php
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 12:28 pm
by Christopher
It should be:
Code: Select all
if (! class_exists('testclass')) {
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 12:30 pm
by Celauran
Also, you may want to take a look at
autoloading
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 12:38 pm
by Revolution
I tried that too
if ( !class_exists('testclass')) {
same result - creates a class everytime.
a.php
-------
include “testclass.php”;
a href=“b.php”;
b.php
-------
include “testclass.php”;
a href=“c.php”;
c.php
-------
include “testclass.php”;
…more code
testclass.php
————————
Code: Select all
if ( !class_exists(’test class')) {
class testclass {
public function __construct($newtestvalue) {
$this->testvalue = $newtestvalue;
public function get_testvalue() {
return $this->testvalue;
public function set_testvalue($new_testvalue) {
$this->testvalue = $newtestvalue;
$testclass = new testclass("1“);
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 1:37 pm
by Celauran
Can you maybe post a little more actual code rather than just the pseudocode above and elaborate on what the problem is? testclass.php defines the class if it doesn't already exist. What makes you think class_exists is failing?
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 3:21 pm
by Christopher
You really should autoload.
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 3:39 pm
by Revolution
I must be doing something wrong - class_exists always fails - and recreates the class.
SAVE AS a.php
<?php echo "A<br>";include "testclass.php";?>
<a href="b.php">Load B</a>
SAVE AS b.php
<?php echo "B<br>";include "testclass.php";?>
<a href="a.php">Load A</a>
SAVE AS testclass.php
<?php
if ( !class_exists('testclass')) {
echo "CREATE CLASS : testclass<br>";
class testclass {}
$testclass = new testclass("1");
}
else {echo "testclass already exists<br>";}
?>
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 4:39 pm
by Celauran
This looks like normal behaviour to me. I don't see anywhere that the class could already have been created. Your a.php and b.php contain nothing but an include statement and a link.
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 6:03 pm
by Revolution
testclass.php included in both a/b.php.
When included... the class as defined in testclass.php is instantiated.
--- $testclass = new testclass("1");
Re: Stuck in limbo
Posted: Wed Mar 26, 2014 10:51 pm
by Christopher
I am confused. How do you think it should work? PHP is a Shared Nothing system. There is no state maintained between scripts. Every script starts from nothing and when it exits there is nothing. So when you load a.php nothing is defined. When a.php includes testclass.php then testclass is defined. The output of the script is sent to the browser and when the script exits then nothing is defined. If you click the link then you load b.php and again nothing is defined. When b.php includes testclass.php then testclass is defined. The output of b.php is sent to the browser and when the script exits then nothing is defined. It is working exactly as expected.
Re: Stuck in limbo
Posted: Thu Mar 27, 2014 6:58 am
by Revolution
Christoper,
I am C#/C++ developer. (from what you said) it appears php objects don't exist at the system level... they exist at the script level.
In other words... phpoo is a Stated language as opposed to a Stateless language.
In (other) OO languages... once instantiated - an object exists and is visible to any script - at any time - until the object dies (is unloaded).
So for example...
assuming a.php instantiated the object:testclass... and the user clicks an href link to load b.php...
b.php would immediately have access to object:testclass.
Another way of looking at this is... a.php might be a login script... it sets property:user_name and property:user_id
b.php would be a display script... in which it would first check if property:user_id is null - if so... the user is not logged in.
How does one pass property values to other scripts - if the object is dies upon script exit ?
EDIT: it seems I have to "serialize" the class ???"... well, I guess I need to did further into this.
EDIT 2: it seems Sessions will serialize / deserialize the current state including objects
- but I can see potential issues using this method... specifically regarding server response time
- as serialization / deserialization requires 2 additional processes... for every page delivery.
There's got to be a better way.
Re: Stuck in limbo
Posted: Thu Mar 27, 2014 9:36 am
by Christopher
Revolution wrote:There's got to be a better way.
The idea with PHP is that there are not custom sub-systems like in Java or C#. If you want to have data persist between requests then use a standard subsystem like a database or file system. PHP does not package a persistence solution for you because it runs on all platforms. You have stateless and stated backwards. The web and webservers are stateless. They do not maintain state between user requests.
With PHP you can communicate information between requests via HTTP parameters or cookies. You can persist data to a database using a key passed via a parameter or cookie. There is also a session system that can persist data between requests -- either in the file system or to a database. REST style interfaces are often used in PHP.
Typically, a PHP script will instantiate all objects needed in each request. Everything is deallocated and all connected disconnected when the request is completed. I understand that this may be different than system you have used in the past. I would recommend that you be open to doing things in a new way. It will make working with PHP much easier.