[thelist] PHP conditionally create a class method?

Ken Snyder kendsnyder at gmail.com
Wed Aug 27 16:26:28 CDT 2008


Stephen Rider wrote:
> ...
>
> 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.
>
> Stephen
>   

As I understand your situation, the usual approach is to use an 
autoloader or a factory class.  For example:

class MyPersonFactory {

  function getInstance() {
    if (STATE == 'TX') {
      include 'Person.tx.php';
    } elseif (STATE == 'CA') {
      include 'Person.ca.php';
    }
    return new Person();
  }

}

Then each of your versions of Person can have different parent classes.



More information about the thelist mailing list