Title: init plugin without construct?
Last modified: April 14, 2019

---

# init plugin without construct?

 *  Resolved [firehold](https://wordpress.org/support/users/firehold/)
 * (@firehold)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/)
 * Hello,
    i am writing my first own plugin and it seems to work more and more. 
   Now i getting problems with wp functions which are not useable in constructor
   but in functions after that. I googled little bit and found that hint: [https://wordpress.stackexchange.com/questions/70055/best-way-to-initiate-a-class-in-a-wp-plugin](https://wordpress.stackexchange.com/questions/70055/best-way-to-initiate-a-class-in-a-wp-plugin)
 * To use an own contructor-function seems to be a coll idea, but i stuck on the
   problem, that i can use variables in the new constructor function and i dont 
   know why.
 * My code:
 *     ```
       add_action( 'plugins_loaded', array( 'MyPlugin', 'plugin_setup' ));
   
       class MyPlugin {
   
           protected $url;
           protected $path;
           protected $basename;
   
           public static function plugin_setup() {
               $class = __CLASS__;
               new $class;
   
               $this->url = plugin_dir_url(__FILE__);
               $this->path =  plugin_dir_path(__FILE__);
               $this->basename = plugin_basename(__FILE__);		
           }
       }
   
       /*
       global $myplugin;
       $myplugin= new MyPlugin();
        */
       ```
   
 * Error:
    `Fatal error: Uncaught Error: Using $this when not in object context 
   in /www/htdocs/XXXXX/wordpress/wp-content/plugins/myplugin/myplugin.php:41 Stack
   trace: #0 /www/htdocs/XXXXX/wordpress/wp-includes/class-wp-hook.php(286): MyPlugin::
   plugin_setup('') #1 /www/htdocs/XXXXX/wordpress/wp-includes/class-wp-hook.php(
   310): WP_Hook->apply_filters(NULL, Array) #2 /www/htdocs/XXXXX/wordpress/wp-includes/
   plugin.php(465): WP_Hook->do_action(Array) #3 /www/htdocs/XXXXX/wordpress/wp-
   settings.php(374): do_action('plugins_loaded') #4 /www/htdocs/XXXXX/wordpress/
   wp-config.php(115): require_once('/www/htdocs/XXXXXX...') #5 /www/htdocs/XXXXX/
   wordpress/wp-load.php(37): require_once('/www/htdocs/XXXXXX...') #6 /www/htdocs/
   XXXXX/wordpress/wp-blog-header.php(13): require_once('/www/htdocs/XXXXXX...')#
   7 /www/htdocs/XXXXX/wordpress/index.php(17): require('/www/htdocs/XXXXXX...')#
   8 {main} thrown in /www/htdocs/XXXXX/wordpress/wp-content/plugins/myplugin/myplugin.
   php on line 41`
    -  This topic was modified 7 years, 2 months ago by [firehold](https://wordpress.org/support/users/firehold/).
    -  This topic was modified 7 years, 2 months ago by [firehold](https://wordpress.org/support/users/firehold/).
    -  This topic was modified 7 years, 2 months ago by [firehold](https://wordpress.org/support/users/firehold/).
    -  This topic was modified 7 years, 2 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/#post-11427106)
 * Since the function is defined as static, these three lines:
 *     ```
       	$this->url = plugin_dir_url(__FILE__);
       	$this->path =  plugin_dir_path(__FILE__);
       	$this->basename = plugin_basename(__FILE__);
       ```
   
 * must instead look like this:
 *     ```
       	self::>url = plugin_dir_url(__FILE__);
       	self::path =  plugin_dir_path(__FILE__);
       	self::basename = plugin_basename(__FILE__);
       ```
   
 * The three variables must also be declared as static. You should instead be adding
   the hook from within a constructor, which would eliminate the need to mess around
   with static functions.
 * I also have no idea why you are instantiating the class from within the static
   function. You might want to do some reading about how to use PHP classes.
 * [https://www.php.net/manual/en/language.oop5.basic.php](https://www.php.net/manual/en/language.oop5.basic.php)
 *  Thread Starter [firehold](https://wordpress.org/support/users/firehold/)
 * (@firehold)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/#post-11427353)
 * Thank you for your answer.
 * As you can see usually i instantiating the class after the class. I outcommented
   it because the author in the empty constructor guide did this inside of the function.
   (
   i linked the post: instantiating )
 * I thought this is needed cause of the different construction way.
 * Is it possible to set the setup-function not static or is that needed to replace
   the constructor.
 * De declaration of my needed variables in the class i did in the shortcode function
   until now yause i wasnt able to use wp classes inside of the constructor, because
   of this i was looking for another solution to setup the class/plugin.
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/#post-11427431)
 * This is what your code should look like:
 *     ```
       class MyPlugin {
       	protected $url;
       	protected $path;
       	protected $basename;
   
       	function __construct() {
       		add_action( 'plugins_loaded', array( $this, 'plugin_setup' ));
       	}
   
       	function plugin_setup() {
       		$this->url = plugin_dir_url(__FILE__);
       		$this->path =  plugin_dir_path(__FILE__);
       		$this->basename = plugin_basename(__FILE__);		
       	}
       }
   
       global $myplugin;
       $myplugin= new MyPlugin();
       ```
   
 * If certain functions you need are not available at the `plugins_loaded` hook,
   you may want to use the `init` or `wp_loaded` hooks instead.
 * (I made a mistake in my previous post. Below are the corrrect lines.)
 *     ```
       	self::$url = plugin_dir_url(__FILE__);
       	self::$path =  plugin_dir_path(__FILE__);
       	self::$basename = plugin_basename(__FILE__);
       ```
   
 *  Thread Starter [firehold](https://wordpress.org/support/users/firehold/)
 * (@firehold)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/#post-11427441)
 * Thank you so much, that looks much more clear than the code before. So in plugin
   setup i can declare all my vars i need in the class.
 * Manny thanks

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘init plugin without construct?’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [firehold](https://wordpress.org/support/users/firehold/)
 * Last activity: [7 years, 2 months ago](https://wordpress.org/support/topic/init-plugin-without-construct/#post-11427441)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
