[thelist] PHP conditionally create a class method?

Stephen Rider evolt_org at striderweb.com
Wed Aug 27 01:32:16 CDT 2008

Hi all --

In PHP I have two classes, a parent and child (child extends parent)

class parent_class {

class child_class extends parent_class {
	// may or may not contain:
	function myfunction() {...}

in parent_class I have this:

function parent_class() {

function create_missing_funcs() {
	if ( ! method_exists( $this, 'myfunctions' ) ) {
		error_log('method doesn\'t exist');
		function myfunction() {
	} else {
		error_log('method exists');

Page crashes with the following in my PHP log:

[27-Aug-2008 01:25:51] hello
[27-Aug-2008 01:25:51] method doesn't exist
[27-Aug-2008 01:25:52] PHP Fatal error:  Call to undefined function:   
myfunction() in /path/to/myscript.php on line 201

So... if ( ! method_exists( $this, 'myfunctions' ) ) returns true,  
which should only happen if the method does _not_ already exist.  But  
then I'm getting an error that the method already exists.

Any idea what I'm doing wrong here?  Should I be doing this differently?

The reason the function may or may not be there is that there are many  
different potential child_classes to the one parent_class.

This is PHP 4, BTW.  (Working in WordPress, which allows min PHP 4.)

Thanks for any info.


More information about the thelist mailing list