0

Given this code:

class test_class {

    function greeting() {
        echo 'Howdy! Test is successful!';
    }

    function greeting_head() {
      //Close PHP tags
    ?>
        <?php greeting() ?>
    <?php //Open PHP tags
    }
}

add_action( 'wp_head', array( 'test_class', 'greeting_head' ) );

greeting() is undefined. How to access if from outside test_class?

UPDATE: I wound up using Sally J.'s solution

PREVIOUS UPDATE: I tried Adnane's solution and it didn't work, but it put me on the trail of the correct answer.

class test_class {

    public static function greeting() {
        return 'Howdy! Test is successful!';
    }

    function greeting_head() {
        echo self::greeting();
    }
}

add_action( 'wp_head', array( 'test_class', 'greeting_head' ) );

Apparrently I was calling a static function in a non-static way. See this question.

1

You're using the static class method call when supplying a callable/callback to add_action():

add_action( 'wp_head', array( 'test_class', 'greeting_head' ) );

So you should make the greeting_head() a static method in your test_class class. But you'd also need to make the greeting() a static method:

class test_class {

    public static function greeting() {
        echo 'Howdy! Test is successful!';
    }

    public static function greeting_head() {
        self::greeting();
    }
}

And unless you use a singleton pattern, you'd need to use the self keyword to access methods and properties from a static method; for example, in the above code, you could see that I'm using self::greeting() and not $this->greeting().

But I wouldn't suggest using static class methods unless absolutely necessary, and you can just leave your class as it is now — except, use $this->greeting() in the greeting_head() method — and use the object method call when supplying the add_action() callback:

class test_class {

    function greeting() {
        echo 'Howdy! Test is successful!';
    }

    function greeting_head() {
        $this->greeting();
    }
}

$test_class = new test_class;

add_action( 'wp_head', array( $test_class, 'greeting_head' ) );

This answer doesn't cover everything about classes in PHP, but I hope it helps you; and if you need further guide, you can search on Stack Overflow or even here on WordPress Stack Exchange.. =)

UPDATE

For completeness,

  1. You're getting this error because you're trying to access a global function named greeting which obviously doesn't exist since PHP threw that error:

    greeting() is undefined.

  2. "How to access if from outside test_class?" — Use the $this keyword: $this->greeting(), just as @Adnane and I mentioned in our original answer. Or use self::greeting() as I stated in my original answer.

    (Update) "if from outside test_class" — Actually, the proper question should be, "how to access the greeting() method from another method in the same class" because you are actually calling test_class::greeting() from test_class::greeting_head(). :)

  3. And your code as in the current question would result in the following PHP notice: (see the add_action() note above)

    Non-static method test_class::greeting_head() should not be called statically

    In addition, you'd get the following PHP fatal error if you simply use $this in your original greeting_head():

    Uncaught Error: Using $this when not in object context

And for this question:

Can you point me to some documentation on the pros and cons of using static class methods?

  • "But I wouldn't suggest using static class methods unless absolutely necessary […]" Thanks. This works. Can you point me to some documentation on the pros and cons of using static class methods? – Chris Jun 13 at 2:28
  • I've just updated the answer; I hope it's more helpful now. But really, searching for "static vs non-static php methods" on Google would give you many good results.. :) – Sally CJ Jun 13 at 3:02
-1

1 - Inside your class you are trying to call the function greeting() thats not a Method of current class so php will try to find this function outside the class and will show Fatal error message if function not exists.

to Call this function inside your class you need to call it as method :

$this->greeting();

2 - Is not a good Idea to use php tags inside class

This is the right Way :

class test_class {

    function greeting() {
        return 'Howdy! Test is successful!';
    }

    function greeting_head() {
        echo $this->greeting();
    }
}

add_action( 'wp_head', array( 'test_class', 'greeting_head' ) );
  • That didn't work. "Fatal error: Uncaught Error: Using $this when not in object context" See next answer for what did. – Chris Jun 13 at 2:10

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.