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 user145078user1450782 Answers
Reset to default 1above 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:
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();
Put global code (or the
$var = new MyClass();
) inside a namespace statement (namespace {}
) with no namespace. Note though, that you need to useNS\MyClass
instead of justMyClass
.<?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 theerror_log
file orwp-content/debug.log
if you enabledWP_DEBUG_LOG
.
- 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.
- 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/