Forum Moderators: coopster

Message Too Old, No Replies

Call to undefined method, only when calling from other class

         

carsten888

2:42 pm on Jan 21, 2009 (gmt 0)

10+ Year Member



I got a form-like application, with a class for each field-type. Including a field-class from an edit-page, works fine. When from within the mainclass I get a 'Call to undefined method'.

main_class.php:

class class_pi{
function include_field_class($fieldtype){
$class_name = 'class_fieldtype_'.$fieldtype;
if(!class_exists($class_name)){
require_once($fieldtype.'.php');
}

function get_field_output($fieldtype){
$class_name = 'class_fieldtype_'.$fieldtype;
if(class_exists($class_name)){
echo 'class exists';
}
$class_fieldtype = new $class_name();
echo $class_fieldtype->test();
}
}

select.php:

class class_fieldtype_select extends class_pi{
function test(){
return 'test';
}
}

edit_page.php:

$fieldtype = 'select';
//include the fields class file
$class_pi->include_field_class($fieldtype);
// get the field-classname
$class_name = 'class_fieldtype_'.$fieldtype;
//make instance of the class
$class_plugin = new $class_name();
/do something, this works
echo $class_plugin->test();


when rendering form content I call get_field_output() in the main class and then get the 'Call to undefined method'.

Grrrr! I've been on this all afternoon and its driving me nuts. Please help me.

enigma1

3:07 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



where is the call that causes the error? You mean after this
echo $class_plugin->test();
if you put
$class_plugin->get_field_output('select');
You get the error?

Of is from within the main_class.php in another member function? In which case you do use the something like
$this->get_field_output('select');

carsten888

11:08 am on Jan 22, 2009 (gmt 0)

10+ Year Member



the call that gives problems is:
$class_fieldtype->test();
in function get_field_output in the main class.
er, so yes you are right, I use $this->get_field_output('select'); from within the mainclass. when called from outside the class it works just fine.

I'm not entirely new to working with classes, but this thing I am totally stuck on.

enigma1

4:53 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well doesn't seem to be the code that you posted so far. It may be something else, is the prototype class file included before the call?

I tried the code here I do not see anything.

here it is in a single file.

class class_pi{
function include_field_class($fieldtype){
$class_name = 'class_fieldtype_'.$fieldtype;
if(!class_exists($class_name)){
//require_once($fieldtype.'.php');
}
}
function get_field_output($fieldtype){
$class_name = 'class_fieldtype_'.$fieldtype;
if(class_exists($class_name)){
echo 'class exists<br />';
}
$class_fieldtype = new $class_name();
echo $class_fieldtype->test();
}
}

//select.php:

class class_fieldtype_select extends class_pi{
function test(){
return 'test';
}
}

//edit_page.php:

$fieldtype = 'select';
//include the fields class file
$class_pi = new class_pi();
$class_pi->include_field_class($fieldtype);
$class_pi->get_field_output('select');
// get the field-classname
$class_name = 'class_fieldtype_'.$fieldtype;
//make instance of the class
$class_plugin = new $class_name();
//do something, this works
//echo $class_plugin->test();

I get the "test" via the $class_pi->get_field_output('select');

carsten888

8:58 am on Jan 26, 2009 (gmt 0)

10+ Year Member



well doesn't seem to be the code that you posted so far. It may be something else, is the prototype class file included before the call?

YEs! in one of the functions the class was already included and that is where it went wrong. thank you all for taking the time to look at my selfinflicked problem.
:)