PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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
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“);
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?
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>";}
?>
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.
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.
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.
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.