0

In my Plugin classes I want to check if the classes are existing (maybe by another plugin) I'm doing it the following way:

/**
* @author schmitt
*
* @file my_util_class.php
*
* @class my_util_class
*
* @brief small message
*
* big message
*/
if ( ! class_exists( 'my_util_class' ) ) :

class my_util_class
{
   /**
    * @brief smalltest
    * bigtestmessage
    */
  public function test() {
       return "hallo";
   }

   //definition of other methods.
}

endif;

until now it works fine. But I want to use doxygen and there I have problems, because doxygen doesn't see the classes inside the if / endif construct.

So I tried the following:

if (class_exists('my_util_class')) {
    return;
}
class my_util_class {
    //...
}

But in this case, "class_exists" everytime gives "true".

I checked the values and get the following:

/*
 * Test 1
 */
$testValue = class_exists('my_util_class'));; //gets true
class my_util_class {
    //...
}

/*
 * Test 2
 */
$testValue = class_exists('my_util_class')); //gets false
if ( ! class_exists( 'my_util_class' ) ) :
    class my_util_class {
        //...
    }
}

I don't understand why. Hope somebody can help me, or give me a better way to test if the class exist?

closed as off-topic by Jacob Peattie, Nathan Johnson, cjbj, EAMann Feb 10 '18 at 3:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    watch out of the fonction name, it's class_exists with a "s" at end – mmm Feb 6 '18 at 13:16
  • sorry, my bad. It was a copy / paste failure by writing this question. In my plugin I have it right, so the problem still exists. – Carsten Schmitt Feb 6 '18 at 13:35
  • I highly recommend you to keep these things separate. One file should only include the class itself, nothing else. Then, in the file that actually uses the class, you can call class_exists() and load the class when necessary. – swissspidy Feb 6 '18 at 13:35
3

In PHP the files are parsed before they are executed, so function and class definitions are 'hoisted' so that they are defined at the beginning of execution, regardless of where they are defined. This doesn't happen when they are conditionally defined, as conditions aren't evaluated until execution.

This, from the PHP manual, was all I could find in terms of documentation of this (but I'm probably searching for the wrong thing, 'hoisting' is a JS term):

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.

http://php.net/manual/en/functions.user-defined.php

EDIT: Another explanation, from here.

Conditionally declared classes must come first. Basically, anything that's at the "top level" of the file is handled directly by the parser while parsing the file, which is the step before the runtime environment executes the code (including new Foo). However, if a class declaration is nested inside a statement like if, this needs to be evaluated by the runtime environment. Even if that statement is guaranteed to be true, the parser cannot evaluate if (true), so the actual declaration of the class is deferred until runtime. And at runtime, if you try to run new Foo before class Foo { }, it will fail.

  • Thank You! I didn't know, that the class and function definitions are getting defined at the beginning, except when they are in a conditional clause. Now I understand what happens! – Carsten Schmitt Feb 7 '18 at 9:47

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