Functions & variables in a declared class not visible
Posted: Fri Jan 09, 2009 2:42 am
My form processing script uses a class for DB functions and can call the class's "update" function in a loop.
The update function itself calls a class to send an email. This would fail when asked to loop with "cannot redeclare class", so I used this (which used to work):-
Now, it fails with "Call to a member function send_email() on a non-object" on the $em->send_email() line the second time the loop uses this code. It seems that the class remains declared but the script can't "see" the function within it. I've since tried it under PHP 4.2.7 and 5.2.5 and both fail.
Here are three test scripts that show this...
THE SCRIPT IN A FILE CALLED test.php
HERE'S THE CLASS CALLED BY test.php
AND HERE'S THE CLASS CALLED FROM test_class1.php
As it stands you get this...
Script start
class_test1 output loop 0
class_test2 output
class_test1 output loop 1
Notice: Undefined variable: test_class2 in /home/ourclien/public_html/test_class1.php on line 12
Fatal error: Call to a member function show_test2_output() on a non-object in /home/ourclien/public_html/test_class1.php on line 12
and if you comment out the "if(!in_array('test_class2',$classes)) {" in test_class1.php you get this...
Script start
class_test1 output loop 0
class_test2 output
class_test1 output loop 1
Fatal error: Cannot redeclare class test_class2 in /home/ourclien/public_html/test_class2.php on line 1
I fear it might be a PHP setup issue but I don't know what to ask of my host.
The update function itself calls a class to send an email. This would fail when asked to loop with "cannot redeclare class", so I used this (which used to work):-
Code: Select all
$classes = get_declared_classes();
if(!in_array('email',$classes)) {
include('classes/class_email.php');
$em = new email; }
}
$em->send_email($to, $subject, $message);Here are three test scripts that show this...
THE SCRIPT IN A FILE CALLED test.php
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include('test_class1.php');
$test_class1 = new test_class1;
echo 'Script start<br />';
for($i=0; $i<3; $i++) {
$test_class1->show_test1_output($i);
}
echo 'Script finish<br />';
?>Code: Select all
<?php class test_class1 {
var $test_string1 = 'class_test1 output';
function show_test1_output($loop) {
echo $this->test_string1.' loop '.$loop.'<br />';
$classes = get_declared_classes();
if(!in_array('test_class2',$classes)) {
include('test_class2.php');
$test_class2 = new test_class2;
}
echo $test_class2->show_test2_output();
}
}
?>Code: Select all
<?php class test_class2 {
var $test_string2 = 'class_test2 output';
function show_test2_output() {
echo $this->test_string2.'<br />';
}
}
?>Script start
class_test1 output loop 0
class_test2 output
class_test1 output loop 1
Notice: Undefined variable: test_class2 in /home/ourclien/public_html/test_class1.php on line 12
Fatal error: Call to a member function show_test2_output() on a non-object in /home/ourclien/public_html/test_class1.php on line 12
and if you comment out the "if(!in_array('test_class2',$classes)) {" in test_class1.php you get this...
Script start
class_test1 output loop 0
class_test2 output
class_test1 output loop 1
Fatal error: Cannot redeclare class test_class2 in /home/ourclien/public_html/test_class2.php on line 1
I fear it might be a PHP setup issue but I don't know what to ask of my host.