-1

i have this i answer in reference but seems have upvoted by many https://wordpress.stackexchange.com/a/48094/145078

but not sure why it is not working for me..

in answer

    class MyClass {
         function __construct() {
              add_action( 'init',array( $this, 'getStuffDone' ) );
         }
         function getStuffDone() {
              // .. This is where stuff gets done ..
         }
    }
    $var = new MyClass();

don't i have to set the visibility?

namespace NS{
class MyClass {
     public function __construct() {
          add_action( 'init',array( $this, 'getStuffDone' ) );
     }
     public function getStuffDone() {
          // .. This is where stuff gets done ..
     }
}
}

$var = new MyClass();

above code does not works for me, is not correct or any mistake from my side?

but this code works fine

add_action('init',function(){
 $lat = new \NS\MyClass();
 $lat->getStuffDone(); //Sorry not $lath
});

i am calling the class file as require get_template_directory(). /myfolder/class/MyClass.php

1

above code does not works for me, is not correct or any mistake from my side?

And it didn't work for me either. Because it throws this fatal error:

PHP Fatal error: No code may exist outside of namespace {} in ...

And that's because the PHP manual says:

No PHP code may exist outside of the namespace brackets except for an opening declare statement.

So your code could be fixed in two ways:

  1. Use the non-bracketed syntax.

    <?php
    namespace NS;
    
    class MyClass {
         public function __construct() {
              add_action( 'init',array( $this, 'getStuffDone' ) );
         }
         public function getStuffDone() {
              // .. This is where stuff gets done ..
         }
    }
    
    $var = new MyClass();
    
  2. Put global code (or the $var = new MyClass();) inside a namespace statement (namespace {}) with no namespace. Note though, that you need to use NS\MyClass instead of just MyClass.

    <?php
    // No code here. (except `declare`)
    
    namespace NS {
    
        class MyClass {
             public function __construct() {
                  add_action( 'init',array( $this, 'getStuffDone' ) );
             }
             public function getStuffDone() {
                  // .. This is where stuff gets done ..
             }
        }
    }
    // No code here. (except another `namespace {...}`)
    
    namespace {
        $var = new NS\MyClass();
    }
    // No code here. (except another `namespace {...}`)
    

UPDATE

Ok, this is what I have in wp-content/themes/my-theme/includes/MyClass.php:

<?php
namespace NS;

class MyClass {
     public function __construct() {
          add_action( 'init', array( $this, 'getStuffDone' ) );
          add_filter( 'the_content', array( $this, 'test' ) );
     }

     public function getStuffDone() {
          error_log( __METHOD__ . ' was called' );
     }

     public function test( $content ) {
          return __METHOD__ . ' in the_content.<hr>' . $content;
     }
}

$var = new MyClass();

And I'm including the file from wp-content/themes/my-theme/functions.php:

require_once get_template_directory() . '/includes/MyClass.php';

Try that out and see if it works for you, because it worked well for me:

  • You'd see NS\MyClass::test in the_content. in the post content (just visit any single post).

  • You'd see NS\MyClass::getStuffDone was called added in the error_log file or wp-content/debug.log if you enabled WP_DEBUG_LOG.

  • Hello, thanks for answer , the answer has to work but i am making some mistake some where ..not able to figure out where i am making that mistake.. i am calling the file in functions.php as require get_template_directory(). /myfolder/class/MyClass.php can that be a problem..sorry for not mentioning this before... – Latheesh V M Villa Oct 20 '18 at 9:20
  • Please check the updated answer. – Sally CJ Oct 20 '18 at 9:53
  • I'm glad it helped you! And I hope you don't mind I edited the answer. (You can see the revision for the edit reason) Also, what was your mistake? You could edit your question, or write an answer, or just reply here. – Sally CJ Oct 20 '18 at 11:29
  • i tried with new files and it works correctly as per your answer but in actual i was having problems with woocommece class that i created it is still not solved but your answer is correct – Latheesh V M Villa Oct 20 '18 at 12:39
  • I see. If the problem is still there, you can post the class code on Pastebin, and I might be able to help you solve/identify the problem. – Sally CJ Oct 22 '18 at 4:29
1
  1. don't i have to set the visibility?

You don't have to, but you should. Class properties and methods without visibility will be implied to be "public", but it's best practice to be explicit when defining members.

  1. above code does not works for me, is not correct or any mistake from my side?

Your code looks fine. I did notice that your class uses the admin_init hook, but your closure example uses init. Bear in mind that admin_init only fires in the WordPress admin, and not on the front-end. It could be possible that your code isn't hooking in at the right time, but that's unlikely.

Try this:

class MyClass {
    public function __construct() {
    }

    public function hooks() {
        add_action( 'admin_init', array( $this, 'doAdminTest' ) );
        add_action( 'init', array( $this, 'doGeneralTest' ) );
    }

    public function doAdminTest() {
        die( 'We are in the admin area.' );
    }

    public function doGeneralTest() {
        die( 'We are in the init hook.' );
    }
}

$var = new MyClass();

add_action( 'plugins_loaded', array( $var, 'hooks' ) );

Now, if you go to /wp-admin/ or your front-end, you should get the result of doGeneralTest. If you comment out the add_action line for init, you should see the result of doAdminTest at /wp-admin/

  • hi, thanks for the answer , it is still not working for me , construct method seems very cool, i am trying to have have frontend(init) with my class..only last method is working me as mentioned in question. i am using namespace will that be problem? – Latheesh V M Villa Oct 19 '18 at 20:35

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.