Jump to content

class funciton/variable visibility


Okewood

Recommended Posts

I think my host has upgraded it's PHP implementation - it's now 5.2.5 and I have a new problem "seeing" variables/functions set up in a class.

Briefly (I hope!!!!)...

My form processing script uses a class for DB functions and can call the "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):-

 

$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 spent hours Googling and have learnt that PHP 5 handles class function and variable declaration differently but should default to the 'public' type for backward compatibility.

 

Help!

Link to comment
https://forums.phpfreaks.com/topic/139986-class-funcitonvariable-visibility/
Share on other sites

Most php4 code works as is under php5. It is more likely that something in your code is php configuration specific and is no longer working or your include statement is failing or your code is using a keyword is that is reserved in php5 or your code is not being executed because a variable is dependent on register_globals or your code is using short open tags... Are you testing this on a system with error_reporting set to E_ALL and display_errors set to ON so that php will show you all the errors it finds?

 

You would need to post all the code that is using the class and the class code to get specific help with what it might be doing or not doing that is php version or php configuration specific.

Good thought - and I found a couple of oddities but they didn't fix the problem.

 

Here's some test code...

THE SCRIPT IN A FILE CALLED test.php

<?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

<?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

<?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

 

Really appreciate your time!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.