1

I have a basic question to beautiful working code I've found (paulund) for making a Custom Control by extending the 'WP_Customize_Control' Class. I am new to PHP, Programming and especially to OOP. So I wonder especially, what the first "condition" do, while the rest seems comprehensibly to me. I have a understanding problem with:

    if ( ! class_exists( 'WP_Customize_Control' ) )
    return NULL;

Read the php manual for "class_exists" and I read in a german blog, that the code above ensures, that we have no conflicts with other Plugins... .

I this is the idea of it, how does it work? Shouldn't the Class exist by Wordpress Core anyway? Sorry for my English. Many thanks in advance.

1

This is a terrible way to make sure you are in the right context. The normal way is to load your class only when it is really needed, not earlier.

A side effect is that the following code cannot be cached, because that line is evaluated on run time, not on compile time. The same applies to function_exists().

Summary: Don't do that.

  • Thank you so much. I think I have to dive deeper into OOP to find a better way to avoid conflicts. Might spl_autoload_register () will provide a better solution? But to be honest... . I will need a few weeks to built up the basic know-how in OOP. Meanwhile and once again many thanks. – Friedrich Siever Feb 21 '17 at 14:05
  • 1
    @FriedrichSiever Yes, do use autoloading. Preferably Coposer or a simple alternative. – fuxia Feb 21 '17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.