Page 1 of 1

Functions & variables in a declared class not visible

Posted: Fri Jan 09, 2009 2:42 am
by Okewood
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):-

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);
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

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 />';
?>
HERE'S THE CLASS CALLED BY test.php

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();      
   }
}
?>
AND HERE'S THE CLASS CALLED FROM test_class1.php

Code: Select all

<?php class test_class2 {
 
   var $test_string2 = 'class_test2 output';
   
   function show_test2_output() {
      echo $this->test_string2.'<br />';      
   }
}
?>
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.

Re: Functions & variables in a declared class not visible

Posted: Fri Jan 09, 2009 7:39 am
by requinix

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);
Let me spell out what happens in that code:
  1. Get a list of declared classes
  2. Check if the "email" class has been defined yet
  3. If not:
    1. include() the file with the class definition
    2. Create a new email class as $em
  4. Call some function on $em
Now ask yourself this: what if the email class has been defined already? Step 3 is completely ignored, right?

The solution involves moving one line of code outside the conditional block (in both instances). That is all.

PS: Wouldn't life be easier if there was a function to check if a class exists?

SOLVED Re: Functions & variables in a declared class not vis

Posted: Fri Jan 09, 2009 8:52 am
by Okewood
Thanks <avatar /> - abolutely right, I'd got fixated on the include() and the instantiation having to be kept together. Quite why it used to work I've no idea!