最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

oop - Using add action for class with construct

matteradmin10PV0评论

i have this i answer in reference but seems have upvoted by many

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

i have this i answer in reference but seems have upvoted by many https://wordpress.stackexchange/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

Share Improve this question edited Oct 20, 2018 at 9:14 asked Oct 19, 2018 at 20:03 user145078user145078
Add a comment  | 

2 Answers 2

Reset to default 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.

  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/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far